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:

  1. first ensure numpy array, myarray, normalised max value @ 1.0.
  2. apply colormap directly myarray.
  3. rescale 0-255 range.
  4. convert integers, using np.uint8().
  5. use image.fromarray().

and you're done:

from pil import image im = image.fromarray(np.uint8(cm.gist_earth(myarray)*255)) 

with plt.savefig():

enter image description here

with im.save():

enter image description here


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -