abdulfatirs

ImageProc.py | Basic Image processing in PIL

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