python - How to convert Numpy array to PIL image applying matplotlib colormap -
i have simple problem cannot find solution it.
i want take numpy 2d array represents grayscale image, , convert rgb pil image while applying of matplotlib colormaps.
i can reasonable png output using pyplot.figure.figimage
command:
dpi = 100.0 w, h = myarray.shape[1]/dpi, myarray.shape[0]/dpi fig = plt.figure(figsize=(w,h), dpi=dpi) fig.figimage(sub, cmap=cm.gist_earth) plt.savefig('out.png')
although adapt want (probably using stringio pil image), wonder if there not simpler way that, since seems natural problem of image visualization. let's say, this:
colored_pil_image = magic_function(array, cmap)
thanks reading!
quite busy 1 liner, here is:
- first ensure numpy array,
myarray
, normalised max value @1.0
. - apply colormap directly
myarray
. - rescale
0-255
range. - convert integers, using
np.uint8()
. - use
image.fromarray()
.
and you're done:
from pil import image im = image.fromarray(np.uint8(cm.gist_earth(myarray)*255))
with plt.savefig()
:
with im.save()
:
Comments
Post a Comment