code: def encode_images(format_dict):
      """b64-encodes images in a displaypub format dict
  
      Perhaps this should be handled in json_clean itself?
  
      Parameters
      ----------
  
      format_dict : dict
          A dictionary of display data keyed by mime-type
  
      Returns
      -------
  
      format_dict : dict
          A copy of the same dictionary,
          but binary image data ('image/png', 'image/jpeg' or 'application/pdf')
          is base64-encoded.
  
      """
  
      # no need for handling of ambiguous bytestrings on Python 3,
      # where bytes objects always represent binary data and thus
      # base64-encoded.
      if py3compat.PY3:
          return format_dict
  
      encoded = format_dict.copy()
  
      pngdata = format_dict.get("image/png")
      if isinstance(pngdata, bytes):
          # make sure we don't double-encode
          if not pngdata.startswith(PNG64):
              pngdata = b2a_base64(pngdata)
          encoded["image/png"] = pngdata.decode("ascii")
  
      jpegdata = format_dict.get("image/jpeg")
      if isinstance(jpegdata, bytes):
          # make sure we don't double-encode
          if not jpegdata.startswith(JPEG64):
              jpegdata = b2a_base64(jpegdata)
          encoded["image/jpeg"] = jpegdata.decode("ascii")
  
      gifdata = format_dict.get("image/gif")
      if isinstance(gifdata, bytes):
          # make sure we don't double-encode
          if not gifdata.startswith((GIF_64, GIF89_64)):
              gifdata = b2a_base64(gifdata)
          encoded["image/gif"] = gifdata.decode("ascii")
  
      pdfdata = format_dict.get("application/pdf")
      if isinstance(pdfdata, bytes):
          # make sure we don't double-encode
          if not pdfdata.startswith(PDF64):
              pdfdata = b2a_base64(pdfdata)
          encoded["application/pdf"] = pdfdata.decode("ascii")
  
      return encoded
  
file_path: catboost/contrib/python/ipykernel/py2/ipykernel/jsonutil.py
imports: from binascii import b2a_base64
  import math
  import re
  import types
  from datetime import datetime
  import numbers
  from ipython_genutils import py3compat
  from ipython_genutils.py3compat import unicode_type, iteritems
  from ipython_genutils.encoding import DEFAULT_ENCODING
label_desc: Use image node_id 1 for calling a global function with example usage: encode_images(format_dict) and returns: encoded, format_dict