Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create a grayscale image

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.

like image 342
Vince Avatar asked Jan 21 '10 17:01

Vince


People also ask

How do I convert color to grayscale?

NOTE: Use the Edit > Edit Colors > Adjust Colors command to convert objects to grayscale and adjust the shades of gray at the same time.


1 Answers

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.

like image 118
Nadia Alramli Avatar answered Oct 14 '22 17:10

Nadia Alramli