title: How to convert a NumPy array to PIL image applying matplotlib colormap
body: <p>I have a simple problem, but I cannot find a good solution to it.</p>
  
  <p>I want to take a NumPy 2D array which represents a grayscale image, and convert it to an RGB PIL image while applying some of the matplotlib colormaps.</p>
  
  <p>I can get a reasonable PNG output by using the <code>pyplot.figure.figimage</code> command:</p>
  
  <pre><code>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')
  </code></pre>
  
  <p>Although I could adapt this to get what I want (probably using StringIO do get the PIL image), I wonder if there is not a simpler way to do that, since it seems to be a very natural problem of image visualization. Let's say, something like this:</p>
  
  <pre><code>colored_PIL_image = magic_function(array, cmap)
  </code></pre>