title: generate 16-bit color map in matplotlib
body: <p>I would like to colorize a 16-bit depth image with a RGB <a href="https://matplotlib.org/3.1.0/tutorials/colors/colormaps.html" rel="nofollow noreferrer">colormap</a> in matplotlib. Technically, 3 channels with 8 bit per channel should be enough to have distinct rgb values for all 2^16 possible depth values.</p>
  
  <p>A standard color map 'viridis' does result in &lt;1000 distinct values even though the original depth image had more than twice of that.</p>
  
  <p>I tried creating the colormap with a larger number of samples <code>plt.get_cmap('viridis', 2**16)</code>, but it is still not enough.</p>
  
  <p>Some code to describe what I am trying to do:</p>
  
  <pre class="lang-py prettyprint-override"><code>def depth_to_rgb(path):   
      depth_map = Image.open(path)
      pixel = np.array(depth_map)
      pixel = (pixel - np.min(pixel)) / np.ptp(pixel)
      cm = plt.get_cmap('viridis', 2**16)
      pixel_colored = np.uint8(np.rint(cm(pixel) * 255))[:, :, :3]
      return Image.fromarray(pixel_colored)
  </code></pre>
  
  <p>I can slightly increase the number of distinct values in the map by creating a custom cm, but it is still not enough: </p>
  
  <pre class="lang-py prettyprint-override"><code>cm = mlp.colors.LinearSegmentedColormap.from_list("",
  ["red", "green", "yellow"], N=2**16)
  </code></pre>
  
  <p>Is there a colormap with enough values or how could I create one?
  Solutions related to the Pillow Image Library are also appreciated.</p>
  
  <p>EDIT </p>
  
  <p>Apparently (thanks to <a href="https://stackoverflow.com/users/4124317/">ImportanceOfBeingErnest</a>) the resulting colormap has indeed 2**16 values, but <code>np.uint8(np.rint(cm(pixel) * 255))</code> caused some of them to fall on the same colors. I only printed the number of distinct colors in the resulting image. I guess I have to do a somehow different mapping, but the original question is answered.</p>