code: def read_image_as_pil(
      image: Union[Image.Image, str, np.ndarray], exif_fix: bool = False
  ):
      """
      Loads an image as PIL.Image.Image.
  
      Args:
          image : Can be image path or url (str), numpy image (np.ndarray) or PIL.Image
      """
      # https://stackoverflow.com/questions/56174099/how-to-load-images-larger-than-max-image-pixels-with-pil
      Image.MAX_IMAGE_PIXELS = None
  
      if isinstance(image, Image.Image):
          image_pil = image
      elif isinstance(image, str):
          # read image if str image path is provided
          try:
              image_pil = Image.open(
                  requests.get(image, stream=True).raw
                  if str(image).startswith("http")
                  else image
              ).convert("RGB")
              if exif_fix:
                  image_pil = exif_transpose(image_pil)
          except:  # handle large/tiff image reading
              try:
                  import skimage.io
              except ImportError:
                  raise ImportError(
                      "Please run 'pip install -U scikit-image imagecodecs' for large image handling."
                  )
              image_sk = skimage.io.imread(image).astype(np.uint8)
              if len(image_sk.shape) == 2:  # b&w
                  image_pil = Image.fromarray(image_sk, mode="1")
              elif image_sk.shape[2] == 4:  # rgba
                  image_pil = Image.fromarray(image_sk, mode="RGBA")
              elif image_sk.shape[2] == 3:  # rgb
                  image_pil = Image.fromarray(image_sk, mode="RGB")
              else:
                  raise TypeError(
                      f"image with shape: {image_sk.shape[3]} is not supported."
                  )
      elif isinstance(image, np.ndarray):
          if image.shape[0] < 5:  # image in CHW
              image = image[:, :, ::-1]
          image_pil = Image.fromarray(image)
      else:
          raise TypeError(
              "read image with 'pillow' using 'Image.open()'"
          )
      return image_pil
  
imports: import copy
  import os
  import random
  import time
  from typing import List, Optional, Union
  import cv2
  import numpy
  import requests
  from PIL import Image
  from sahi.utils.file import Path
label_desc: Use image node_id 5 for calling a global function with example usage: read_image_as_pil(image, exif_fix) and returns: image_pil