0

It's pretty easy to turn an image into a greyscale version of itself in PIL. But how do I then turn that image into a list of its values, row by row. Not an array. Just a list. In this case the image is 28 x 28.

But assume its 2 x 2 and its greyscale looked like this:

214, 61

131, 121

then, I would want it to just be a list like this:

[214, 61, 131, 121]

1 Answers1

0

Like this:

from PIL import Image

# Create 2x2 grey image
im = Image.new('L', (2,2), color=128)

print(list(im.getdata()))

Output:

[128, 128, 128, 128]

Note that the value returned by getdata() is already an iterator, so there's no need to make it into a list if you just want to iterate over it:

for px in im.getdata():
   print(px)

will work just fine.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432