How to save a Numpy array output of an autoencoder as an image [closed]

I have a 256*256*3 numpy array "SP" out of an autoencoder decoder layer which I want to save and open as an .jpg image. I used something like the following python code snippets:

img = Image.fromarray(SP, 'RGB')
img.save('my.jpg')
img.show()


However I have noticed the array "img" is 256*256 in dimension and the image is just a noise. What can be the right way to display the image? I have attached the array as a output.npy file: ---> https://ufile.io/410iu

• Double check SP values range (0 to 255) or (0. to 1.) Oct 30, 2018 at 7:09
• I'd recommend to use pickle. More info can be found in here: pythoncentral.io/how-to-pickle-unpickle-tutorial Oct 30, 2018 at 7:29
• Yes, I should multiply all values of SP by 255 @krayyem Oct 31, 2018 at 6:19
• @SyedAliHamza, hi pickling will convert the data to binary or some encrypted form if I am not wrong? But I need RGB image... Oct 31, 2018 at 6:32
• it does convert, but when you read it again with pickle, it should be back in its original form. Oct 31, 2018 at 11:45

That is right. The size you see is the Frame Size of the image i.e. height and width. It does not refer to the dimensionality of a color image array. To see it try this

rgb = np.zeros((255, 255, 3), dtype=np.uint8)
img = Image.fromarray(rgb, 'RGB')
r = img.getchannel("R")
g = img.getchannel("G")
b = img.getchannel("B")
print(np.array(r.getdata()))
print(np.array(g.getdata()))
print(np.array(b.getdata()))


where output is

[  0   1   2 ... 252 253 254]
[55 55 55 ... 55 55 55]
[  1   0 255 ...   5   4   3]


So you have 3 dimensions (or colors). And the point about noise is in dtype=np.uint8. Convert your array to this and it will work.

You could also simply try

from scipy.misc import imsave
imsave("file_name.jpg", SP)


It does the job.

Good Luck!