简体   繁体   中英

Error when transforming numpy array to PIL Image

I have a variable img which is a int64 numpy.array with sizes 28x28. Its content looks like this:

[...]
[  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   0   0   0   0]
[  0   0   0   0   0   0   0   0   0  68 154 188 176 254 254 254 254 254
  227 106  17   0   0   0   0   0   0   0]
[...]

I want to convert the array to a PIL image. To do so I call img = Image.fromarray(img, mode='L') but the output I get is only 0s while it is obvious that it shouldn't be like that. I have checked the mode options and seems like L is correct. Also checked other answers inside stackoverflow and couldn't find something that reproduces this particular problem.

L (8-bit pixels, black and white)

Why is this "simple" piece of code given an unexpected behaviour?

Thanks in advance!

As @Divakar pointed out, the data types were not coherent.

Just by adding np.uint8() it works:

img = Image.fromarray(np.uint8(img), mode='L')

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