Getting list of pixel values from PIL
Guys, I'm looking for a bit of assistance. I'm a newbie programmer and one of the problems I'm having at the minute is trying to convert a black & white .jpg
image into a list which I can then modulate into an audio signal. This is part of a lager project to create a python SSTV program.
I have imported the PIL module and am trying to call the built-in function: list(im.getdata())
. When I call it, python crashes. Is there some way of breaking down the image (always 320x240) into 240 lines to make the computations easier? Or am I just calling the wrong function.
If anyone has any suggestions please fire away. If anyone has experience of generating modulated audio tones using python I would gladly accept any 'pearls of wisdom' they are willing to impart. Thanks in advance
Solution 1:
Python shouldn't crash when you call getdata(). The image might be corrupted or there is something wrong with your PIL installation. Try it with another image or post the image you are using.
This should break down the image the way you want:
from PIL import Image
im = Image.open('um_000000.png')
pixels = list(im.getdata())
width, height = im.size
pixels = [pixels[i * width:(i + 1) * width] for i in xrange(height)]
Solution 2:
If you have numpy installed you can try:
data = numpy.asarray(im)
(I say "try" here, because it's unclear why getdata()
isn't working for you, and I don't know whether asarray
uses getdata, but it's worth a test.)