I have an array of 50x50 elements of which each is either True or False - this represents a 50x50 black and white image.
I can't convert this into image? I've tried countless different functions and none of them work.
import numpy as np
from PIL import Image
my_array = np.array([[True,False,False,False THE DATA IS IN THIS ARRAY OF 2500 elements]])
im = Image.fromarray(my_array)
im.save("results.jpg")
^ This one gives me: "Cannot handle this data type".
I've seen that PIL has some functions but they only convert a list of RGB pixels and I have a simple black and white array without the other channels.
First you should make your array 50x50 instead of a 1d array:
my_array = my_array.reshape((50, 50))
Then, to get a standard 8bit image, you should use an unsigned 8-bit integer dtype:
my_array = my_array.reshape((50, 50)).astype('uint8')
But you don't want the True
s to be 1
, you want them to be 255
:
my_array = my_array.reshape((50, 50)).astype('uint8')*255
Finally, you can convert to a PIL image:
im = Image.fromarray(my_array)
I'd do it all at once like this:
im = Image.fromarray(my_array.reshape((50,50)).astype('uint8')*255)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments