How to create image from numpy float32 array?
I would agree with DavidG's answer being a quick solution to plot an image from a numpy array. However, if you have a very good reason for sticking with PIL.Image
, the closest approach to what you've already done would be something like this:
from PIL import Imageimport numpy as npslice56 = np.random.random((226, 226))# convert values to 0 - 255 int8 formatformatted = (slice56 * 255 / np.max(slice56)).astype('uint8')img = Image.fromarray(formatted)img.show()
It will then produce something like below given random numbers:
scipy.misc
is my favorite way of doing it. It's cleaner and it is done in just one line.Look how nice it is:
import scipy.miscimg = scipy.misc.toimage(slice56, mode='L')
Please check here for the official docs.
Thanks to John Titus Jungao
the problem solved. The array was 1D so I did the following:
from PIL import Imageimport numpy as npslice56 = slice56.reshape((226,226))formatted = (slice56 * 255 / np.max(slice56)).astype('uint8')img = Image.fromarray(formatted)img.save('slice56.png')
and that's it.