0

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!

2
  • 1
    Shouldn't the input be of dtype np.uint8? Commented Aug 18, 2017 at 14:43
  • You're correct! Thank you so much (it actually makes so much sense) :) Commented Aug 18, 2017 at 14:49

1 Answer 1

1

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')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.