Reputation: 3395
I am reading binary data from one file that specifies intensity values across x and y coordinates (not a open-source image format) and want to convert it to a PNG image (or other widely supported format). I have the data loaded into an array (using the array module) where each element is a integer from 0 to 255. To save this to a PNG I can create a 3 item tuple of each element (x) like so:
t = (x, x, x)
add apply it across the array using the map(), then save the image using putdata(). However, the conversion to the array of tuples takes a long time (few minutes). Is there a way to specify the rgb value using only one integer (not a tuple). I guessing an alternative would be to use NumPy, but I don't know where to start, so any help in this regard would also be appreciated.
Thanks in advance for the help.
Upvotes: 14
Views: 29568
Reputation: 114933
When you create the new image, give it the mode L:
im = Image.new('L', size)
im.putdata([x1, x2, x3, ...])
Where data is a list of values not tuples.
Upvotes: 23
Reputation: 9759
There are several ways to do this, but if you already have the data in memory, look into using Image.frombuffer
or Image.fromstring
using the 'L'
mode (for 8-bit greyscale data).
Upvotes: 3