code: def heatmap_to_image(heatmap):
      # type: (np.ndarray) -> Image
      """
      Convert the numpy array ``heatmap`` to a Pillow image.
  
      Parameters
      ----------
      heatmap : numpy.ndarray
          Rank 2 grayscale ('L') array or rank 3 coloured ('RGB' or RGBA') array,
          with values in interval [0, 1] as floats.
  
  
      :raises TypeError: if ``heatmap`` is not a numpy array.
      :raises ValueError: if ``heatmap`` does not contain values as floats in the interval [0, 1].
      :raises ValueError: if ``heatmap`` rank is neither 2 nor 3.
      :raises ValueError: if rank 3 ``heatmap`` does not have 4 (RGBA) or 3 (RGB) channels.
  
  
      Returns
      -------
      heatmap_image : PIL.Image.Image
          Heatmap as an image with a suitable mode.
      """
      _validate_heatmap(heatmap)
      rank = len(heatmap.shape)
      if rank == 2:
          mode = "L"
      elif rank == 3:
          channels = heatmap.shape[2]
          if channels == 4:
              mode = "RGBA"
          elif channels == 3:
              mode = "RGB"
          else:
              raise ValueError(
                  "Rank 3 heatmap must have 4 channels (RGBA), "
                  "or 3 channels (RGB). "
                  "Got shape with {} channels".format(channels)
              )
      else:
          raise ValueError(
              "heatmap must have rank 2 (L, grayscale) "
              "or rank 3 (RGBA, colored). "
              "Got: %d" % rank
          )
      heatmap = (heatmap * 255).astype("uint8")  # -> [0, 255] int
      return Image.fromarray(heatmap, mode=mode)
  
file_path: eli5/eli5/formatters/image.py
imports: from __future__ import absolute_import
  from typing import Union, Optional, Callable
  import numpy
  from PIL import Image
  import matplotlib.cm
  from eli5.base import Explanation
label_desc: Use image node_id 2 for calling a global function with example usage: heatmap_to_image(heatmap) and returns: Image