code: def save_image(
      y: np.ndarray | _Image.Image | str | Path, cache_dir: str
  ):
      # numpy gets saved to png as default format
      # PIL gets saved to its original format if possible
      if isinstance(y, np.ndarray):
          path = processing_utils.save_img_array_to_cache(
              y, cache_dir=cache_dir
          )
      elif isinstance(y, _Image.Image):
          fmt = y.format
          try:
              path = processing_utils.save_pil_to_cache(
                  y,
                  cache_dir=cache_dir,
                  format=fmt,  # type: ignore
              )
          # Catch error if format is not supported by PIL
          except (KeyError, ValueError):
              path = processing_utils.save_pil_to_cache(
                  y, cache_dir=cache_dir, format="png"
              )
      elif isinstance(y, Path):
          path = str(y)
      elif isinstance(y, str):
          path = y
      else:
          raise ValueError(
              "Cannot process this value as an Image, it is of type: "
              + str(type(y))
          )
  
      return path
  
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 2 for calling a global function with example usage: save_image(y, cache_dir) and returns: path