1
$\begingroup$

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

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

1 Answer 1

1
$\begingroup$

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!

$\endgroup$
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.