简体   繁体   中英

Converting an array of floating point pixels in [0,1] to grayscale image in python

I have a 400x1 numpy array of floating point numbers in the interval [0,1] representing the pixels of an image, ie, if resized to 20x20 it would be the desired picture. I want to convert this to a grayscaled image in Python. To completely reproduce my error, I have put my array in this pastebin link . My code is

pixels = np.array(pixels)
img = Image.fromarray(pixels.reshape((20,20)), mode='LA')
img = img.resize((140, 140), Image.LANCZOS)
img.show()

which results in

在此处输入图片说明

The image is supposed to be a 7.

Assuming 'pixels' is a python list of floats between 0 and 1,

pixels = 255 * (1.0 - pixels)
pixels.resize((20,20))
im = Image.fromarray(pixels.astype(np.uint8), mode='L')
im = im.resize((140, 140))
im.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM