code: def ret_and_save_image(
      image: Image.Image,
      output_path: Optional[str],
      src_mode: Optional[str] = None,
  ) -> Image.Image:
      if src_mode is not None:
          image = image.convert(src_mode)
  
      if output_path is not None:
          if (
              any(
                  output_path.endswith(extension)
                  for extension in JPEG_EXTENSIONS
              )
              or image.mode == "CMYK"
          ):
              image = image.convert("RGB")
  
          utils.validate_output_path(output_path)
          image.save(output_path)
  
      return image
  
file_path: AugLy/augly/image/utils/utils.py
imports: import functools
  import json
  import math
  import os
  from typing import List, Optional, Tuple, Union
  import numpy
  from augly import utils
  from PIL import Image
label_desc: Use image node_id 2 for calling a global function with example usage: ret_and_save_image(image, output_path, src_mode) and returns: image