code: def save_pil_to_file(self, pil_image, dir=None, format="png"):
      already_saved_as = getattr(pil_image, "already_saved_as", None)
      if already_saved_as and os.path.isfile(already_saved_as):
          register_tmp_file(shared.demo, already_saved_as)
          filename = already_saved_as
  
          if not shared.opts.save_images_add_number:
              filename += f"?{os.path.getmtime(already_saved_as)}"
  
          return filename
  
      if shared.opts.temp_dir != "":
          dir = shared.opts.temp_dir
      else:
          os.makedirs(dir, exist_ok=True)
  
      use_metadata = False
      metadata = PngImagePlugin.PngInfo()
      for key, value in pil_image.info.items():
          if isinstance(key, str) and isinstance(value, str):
              metadata.add_text(key, value)
              use_metadata = True
  
      file_obj = tempfile.NamedTemporaryFile(
          delete=False, suffix=".png", dir=dir
      )
      pil_image.save(
          file_obj, pnginfo=(metadata if use_metadata else None)
      )
      return file_obj.name
  
file_path: stable-diffusion-webui/modules/ui_tempdir.py
imports: import os
  import tempfile
  from collections import namedtuple
  from pathlib import Path
  import gradio.components
  from PIL import PngImagePlugin
  from modules import shared
label_desc: Use image node_id 3 for calling a global function with example usage: save_pil_to_file(pil_image, dir, format) and returns: file_obj, filename