PIL.Image.fromarray produces distorted image after np.reshape

Viewed 2742

I have an array (numpy) of shape (10000,1296) that I want to save as 10,000 images of 36x36 size. All values in the array are in the range (0,255). So I have used this code to do that (which worked well):

for i, line in enumerate(myarray):
    img = Image.new('L',(36,36))
    img.putdata(line)
    img.save(str(i)+'.png')

I wanted to replace this code using the Image.fromarray method, but the resulting pictures are distorted beyond recognition compared to the original method. First I tried this:

myarray = myarray.reshape(10000,36,36)
for i, line in enumerate(myarray):
    img = Image.fromarray(line, 'L')
    img.save(str(i)+'.png')

Which didn't work. So to debug I thought I would try just one item and did this:

Image.fromarray(myarray[0].reshape(36,36), 'L').save('test.png')

And again - garbled distorted image.

So I figured either fromarray isn't working like I thought it should, or my reshape is too naive and messes up the data, but I am not able to fix this. Any ideas are welcome.

1 Answers
Related