code: def format_as_image(
      expl,  # type: Explanation
      resampling_filter=Image.LANCZOS,  # type: int
      colormap=matplotlib.cm.viridis,  # type: Callable[[np.ndarray], np.ndarray]
      alpha_limit=0.65,  # type: Optional[Union[float, int]]
  ):
      # type: (...) -> Image
      """format_as_image(expl, resampling_filter=Image.LANCZOS, colormap=matplotlib.cm.viridis, alpha_limit=0.65)
  
      Format a :class:`eli5.base.Explanation` object as an image.
  
      Note that this formatter requires ``matplotlib`` and ``Pillow`` optional dependencies.
  
  
      :param Explanation expl:
          :class:`eli5.base.Explanation` object to be formatted.
          It must have an ``image`` attribute with a Pillow image that will be overlaid.
          It must have a ``targets`` attribute, a list of :class:`eli5.base.TargetExplanation` \
          instances that contain the attribute ``heatmap``, \
          a rank 2 numpy array with float values in the interval [0, 1].
          Currently ``targets`` must be length 1 (only one target is supported).
  
  
          :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 TypeError: if ``image`` is not a Pillow image.
  
      :param resampling_filter:
          Interpolation ID or Pillow filter to use when resizing the image.
  
          Example filters from PIL.Image
              * ``NEAREST``
              * ``BOX``
              * ``BILINEAR``
              * ``HAMMING``
              * ``BICUBIC``
              * ``LANCZOS``
  
          See also `<https://pillow.readthedocs.io/en/stable/handbook/concepts.html#filters>`_.
  
          *Note that these attributes are integer values*.
  
          Default is ``PIL.Image.LANCZOS``.
      :type resampling_filter: int, optional
  
      :param colormap:
          Colormap scheme to be applied when converting the heatmap from grayscale to RGB.
          Either a colormap from matplotlib.cm, 
          or a callable that takes a rank 2 array and 
          returns the colored heatmap as a [0, 1] RGBA numpy array.
  
          Example colormaps from matplotlib.cm
              * ``viridis``
              * ``jet``
              * ``binary``
  
          See also https://matplotlib.org/gallery/color/colormap_reference.html.
  
          Default is ``matplotlib.cm.viridis`` (green/blue to yellow).
      :type colormap: callable, optional
  
      :param alpha_limit:
          Maximum alpha (transparency / opacity) value allowed 
          for the alpha channel pixels in the RGBA heatmap image.
  
          Between 0.0 and 1.0.
  
          Useful when laying the heatmap over the original image, 
          so that the image can be seen over the heatmap.
  
          Default is 0.65.
  
  
          :raises ValueError: if ``alpha_limit`` is outside the [0, 1] interval.
          :raises TypeError: if ``alpha_limit`` is not float, int, or None.
      :type alpha_limit: float or int, optional
  
  
      Returns
      -------
      overlay : PIL.Image.Image
          PIL image instance of the heatmap blended over the image.
      """
      image = expl.image
      # validate image
      if not isinstance(image, Image.Image):
          raise TypeError(
              "Explanation image must be a PIL.Image.Image instance. "
              "Got: {}".format(image)
          )
      if image.mode != "RGBA":
          # normalize to 'RGBA'
          image = image.convert("RGBA")
  
      if not expl.targets:
          # no heatmaps
          return image
      else:
          assert len(expl.targets) == 1
          heatmap = expl.targets[0].heatmap
          _validate_heatmap(heatmap)
  
      # The order of our operations is: 1. colorize 2. resize
      # as opposed: 1. resize 2. colorize
  
      # save the original heatmap values
      heatvals = heatmap
      # apply colours to the grayscale array
      heatmap = _colorize(
          heatmap, colormap=colormap
      )  # -> rank 3 RGBA array
  
      # make the alpha intensity correspond to the grayscale heatmap values
      # cap the intensity so that it's not too opaque when near maximum value
      _update_alpha(
          heatmap, starting_array=heatvals, alpha_limit=alpha_limit
      )
  
      heatmap = expand_heatmap(
          heatmap, image, resampling_filter=resampling_filter
      )
      overlay = _overlay_heatmap(heatmap, image)
      return overlay
  
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 1 for calling a global function with example usage: format_as_image(expl, resampling_filter, colormap, alpha_limit) and returns: overlay, image