Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Standard libraries being imported
- from __future__ import division
- from PIL import Image # Importing Python Image Library
- from operator import add
- import numpy
- # Function to process image and turn into RGB values
- def processor(x):
- """ Input 'x' should be a string name for the image of format TIFF or PNG -
- not JPEG.
- Output is an array corresponding to the image, each RGB pixel has been
- replaced with a singular grayscale value. """
- im = Image.open(x)
- pix = im.load()
- size = im.size # Gets the width and height of the image to iterate over
- # Creating an array to store the RGBA values from the image and their coord
- image_array = numpy.zeros((size[0],size[1]), dtype=object)
- for i in range(size[0]):
- for j in range(size[1]):
- pixel = pix[i,j] # Pulling the RGBA data from the image
- # Converting 'sRGB' to greyscale using CIE 1931 Linear Luminance
- luminance = (0.2126 * pixel[0]) + (0.7152 * pixel[1]) + (0.0722 * pixel[2])
- image_array[i,j] = luminance # Stores value into matching coord in array
- return image_array
- #Testing and running the code
- if __name__ == '__main__':
- import image_analyser # Imports current module
- image_analyser.processor("FILE NAME HERE")
Add Comment
Please, Sign In to add comment