Convert black and white array into an image in python?

user3010273

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.

askewchan

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 Trues 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.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert black and white image to array with 3 dimensions in python?

Convert a black and white image to array of numbers?

Convert RGB image to black and white

Convert list of lists of 1 and 0 in black and white image in Python

Convert Black and White Image with Color Palette using Python

Python - Remove(convert to white) non black pixels of an image

Python convert color image to black text on white background for OCR

crop image, convert to black and white and sharpen it

Convert all white pixels in the image into black pixels

How to convert a photo to a black and white image by ImageMagick?

Is there a way to convert an image to black and white pixel by pixel?

Load a black and white image into an binary array

How to create a numpy array for a black/white image?

Convert White Pixels to Black in OpenCV python

Convert RGB to black OR white

convert RGB to black and white

Java - Convert Image to black and white - fails with bright colors

Convert 2D binary matrix to black/white image in java

Best RGB combination to convert image into Black and White "threshold"

Flutter / Dart : convert image to 1 bit black and white

Convert color image to black and white in Base64 format ( typescript )

How to convert a grayscale image to pure black and white in php?

Can't convert black&white image to grayscale in Opencv

Heroku black and white image

Using python PIL to turn a RGB image into a pure black and white image

Using python PIL to turn a RGB image into a pure black and white image

How to convert the background of the entire image to white when both white and black backgrounds are present?

Create watermark from black and white image using python

Python OpenCV, check if thresholded image is mostly white or black