Guest User

Untitled

a guest
Nov 21st, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. # Standard libraries being imported
  2. from __future__ import division
  3. from PIL import Image # Importing Python Image Library
  4. from operator import add
  5.  
  6. import numpy
  7.  
  8. # Function to process image and turn into RGB values
  9. def processor(x):
  10. """ Input 'x' should be a string name for the image of format TIFF or PNG -
  11. not JPEG.
  12. Output is an array corresponding to the image, each RGB pixel has been
  13. replaced with a singular grayscale value. """
  14. im = Image.open(x)
  15. pix = im.load()
  16. size = im.size # Gets the width and height of the image to iterate over
  17.  
  18. # Creating an array to store the RGBA values from the image and their coord
  19. image_array = numpy.zeros((size[0],size[1]), dtype=object)
  20.  
  21. for i in range(size[0]):
  22. for j in range(size[1]):
  23. pixel = pix[i,j] # Pulling the RGBA data from the image
  24. # Converting 'sRGB' to greyscale using CIE 1931 Linear Luminance
  25. luminance = (0.2126 * pixel[0]) + (0.7152 * pixel[1]) + (0.0722 * pixel[2])
  26. image_array[i,j] = luminance # Stores value into matching coord in array
  27. return image_array
  28.  
  29. #Testing and running the code
  30. if __name__ == '__main__':
  31. import image_analyser # Imports current module
  32.  
  33. image_analyser.processor("FILE NAME HERE")
Add Comment
Please, Sign In to add comment