Advertisement
abdulfatirs

ImageProc.py | Basic Image processing in PIL

Apr 13th, 2014
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.61 KB | None | 0 0
  1. #! /usr/bin/env python
  2. # ImageProc.py
  3. # Author: Abdul Fatir
  4. # E-Mail: abdulfatirs@gmail.com
  5.  
  6. # The Image module contains the basic functions of image modification
  7. from PIL import Image
  8. # The ImageFilter module contains the filters that can be applied on the image
  9. from PIL import ImageFilter
  10.  
  11. # Example 1 : Creating a new image and saving it with custom pixel data
  12. Width,Height = ImageSize = (1280,800)
  13. # Image.new(IMAGE_MODE, IMAGE_SIZE) where IMAGE_SIZE is a two tuple containing (Width,Height)
  14. # IMAGE_MODE is string and can be "RGB" for RGB Images, "L" for Gray-scale and few more.
  15. customPixImage = Image.new("RGB", ImageSize)
  16. PixelData = customPixImage.load()
  17. # Now we add our custom pixel data to the 2D array PixelData
  18. for y in range(Height):
  19.     for x in range(Width):
  20.         PixelData[x,y] = (100, x*255/1280, y*255/800) # PixelData[x,y] = (R,G,B)
  21. customPixImage.save("CustomPixelImage.png")
  22. del customPixImage
  23.  
  24. # Example 2 : Opening an image, cropping, resizing and converting it to thumbnail
  25. ImgName = 'church.png'
  26. # Image.open(FILE_NAME) is used to open an image
  27. churchImage = Image.open(ImgName)
  28. W,H = churchImage.size
  29. print '[',ImgName,'] - Format: ',churchImage.format,', Mode: ',churchImage.mode,', Size: ',churchImage.size
  30.  
  31. # The crop(box) function takes a four tuple containing co-ordinates of top-left and bottom-right
  32. croppedImage=churchImage.crop((0,0,300,200))
  33. croppedImage.save('cropped.png')
  34. del croppedImage
  35.  
  36. # The resize((W,H)) function takes a two tuple containing width and height
  37. resizedImage=churchImage.resize((300,200))
  38. resizedImage.save('resized.png')
  39. del resizedImage
  40.  
  41. # The thumbnail((W,H)) function takes a two tuple containing width and height
  42. thumb=churchImage.copy()
  43. thumb.thumbnail((200,200))
  44. thumb.save('thumb.png')
  45. del thumb
  46.  
  47. # Example 3 : Converting images to different modes
  48. grayscale = churchImage.convert("L")
  49. grayscale.save('grayscale.png')
  50. del grayscale
  51.  
  52. bw = churchImage.convert("1")
  53. bw.save('blackwhite.png')
  54. del bw
  55.  
  56. cmyk = churchImage.convert("CMYK")
  57. cmyk.save('cmyk.jpg')
  58. del cmyk
  59.  
  60. # Example 4 : Applying filters to images
  61.  
  62. blur = churchImage.filter(ImageFilter.BLUR)
  63. blur.save('blurred.png')
  64. del blur
  65.  
  66. emboss = churchImage.filter(ImageFilter.EMBOSS)
  67. emboss.save('embossed.png')
  68. del emboss
  69.  
  70. contour = churchImage.filter(ImageFilter.CONTOUR)
  71. contour.save('contoured.png')
  72. del contour
  73.  
  74. detail = churchImage.filter(ImageFilter.DETAIL)
  75. detail.save('detail.png')
  76. del detail
  77.  
  78. edge= churchImage.filter(ImageFilter.EDGE_ENHANCE)
  79. edge.save('edgeenhanced.png')
  80. del edge
  81.  
  82. smooth= churchImage.filter(ImageFilter.SMOOTH)
  83. smooth.save('smooth.png')
  84. del smooth
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement