code: def format_image(
      im: _Image.Image | None,
      type: Literal["numpy", "pil", "filepath"],
      cache_dir: str,
      name: str = "image",
      format: str = "png",
  ) -> np.ndarray | _Image.Image | str | None:
      """Helper method to format an image based on self.type"""
      if im is None:
          return im
      fmt = im.format
      if type == "pil":
          return im
      elif type == "numpy":
          return np.array(im)
      elif type == "filepath":
          try:
              path = processing_utils.save_pil_to_cache(
                  im,
                  cache_dir=cache_dir,
                  name=name,
                  format=fmt or format,  # type: ignore
              )
          # Catch error if format is not supported by PIL
          except (KeyError, ValueError):
              path = processing_utils.save_pil_to_cache(
                  im,
                  cache_dir=cache_dir,
                  name=name,
                  format="png",  # type: ignore
              )
          return path
      else:
          raise ValueError(
              "Unknown type: "
              + str(type)
              + ". Please choose from: 'numpy', 'pil', 'filepath'."
          )
  
file_path: gradio/gradio/image_utils.py
imports: from __future__ import annotations
  from pathlib import Path
  from typing import Literal
  import numpy
  from PIL import Image
  from gradio import processing_utils
label_desc: Use image node_id 1 for calling a global function with example usage: format_image(im, type, cache_dir, name, format) and returns: im, im, np, path