
Here's the output from the first example of access:Īnd here the output from the second example: % * %. Here's an enlarged version of a small 24x24 RGB eggs.png image I used for testing: # Here's another more compact representation. Print(' '.join(''.format(value) for value in row)) # At this point the image's pixels are all in memory and can be accessed

# convert that to 2D list (list of lists of integers)ĭata = for offset in range(0, WIDTH*HEIGHT, WIDTH)] Img = Image.open('eggs.png').convert('L') # convert image to 8-bit grayscaleĭata = list(img.getdata()) # convert image data to a list of integers You can convert the image data into a Python list (or list-of-lists) like this: from PIL import Image If you are looping over all of the pixels in an image, there is likely a faster way using other parts of the Pillow API.

The pixel values are not stored as integers, but pixel objects which cannot be further mathematically manipulated and are therefore useless.Īccessing individual pixels is fairly slow.Img = Image.open('eggs.png').convert('1') I have found the Pillow library to be the most useful in this task, by iterating over each pixel and appending its value to an array from PIL import Image I plan on using this array for creating a MNIST-like bytefile of the picture, that can be recognized by Tensor-Flow handwriting recognition algorithms.

I am trying to create a python program which takes a grayscale, 24*24 pixel image file (I haven't decided on the type, so suggestions are welcome) and converts it to a list of pixel values from 0 (white) to 255 (black).
