code: def _pillow2array(img, flag="color", channel_order="bgr"):
      """Convert a pillow image to numpy array.
  
      Args:
          img (:obj:`PIL.Image.Image`): The image loaded using PIL
          flag (str): Flags specifying the color type of a loaded image,
              candidates are 'color', 'grayscale' and 'unchanged'.
              Default to 'color'.
          channel_order (str): The channel order of the output image array,
              candidates are 'bgr' and 'rgb'. Default to 'bgr'.
  
      Returns:
          np.ndarray: The converted numpy array
      """
      channel_order = channel_order.lower()
      if channel_order not in ["rgb", "bgr"]:
          raise ValueError(
              'channel order must be either "rgb" or "bgr"'
          )
  
      if flag == "unchanged":
          array = np.array(img)
          if array.ndim >= 3 and array.shape[2] >= 3:  # color image
              array[:, :, :3] = array[:, :, (2, 1, 0)]  # RGB to BGR
      else:
          # Handle exif orientation tag
          if flag in ["color", "grayscale"]:
              img = ImageOps.exif_transpose(img)
          # If the image mode is not 'RGB', convert it to 'RGB' first.
          if img.mode != "RGB":
              if img.mode != "LA":
                  # Most formats except 'LA' can be directly converted to RGB
                  img = img.convert("RGB")
              else:
                  # When the mode is 'LA', the default conversion will fill in
                  #  the canvas with black, which sometimes shadows black objects
                  #  in the foreground.
                  #
                  # Therefore, a random color (124, 117, 104) is used for canvas
                  img_rgba = img.convert("RGBA")
                  img = Image.new("RGB", img_rgba.size, (124, 117, 104))
                  img.paste(
                      img_rgba, mask=img_rgba.split()[3]
                  )  # 3 is alpha
          if flag in ["color", "color_ignore_orientation"]:
              array = np.array(img)
              if channel_order != "rgb":
                  array = array[:, :, ::-1]  # RGB to BGR
          elif flag in ["grayscale", "grayscale_ignore_orientation"]:
              img = img.convert("L")
              array = np.array(img)
          else:
              raise ValueError(
                  'flag must be "color", "grayscale", "unchanged", '
                  f'"color_ignore_orientation" or "grayscale_ignore_orientation"'
                  f" but got {flag}"
              )
      return array
  
file_path: lavis/lavis/common/annotator/uniformer/mmcv/image/io.py
imports: import io
  import os.path
  from pathlib import Path
  import cv2
  import numpy
  from cv2 import IMREAD_COLOR, IMREAD_GRAYSCALE, IMREAD_IGNORE_ORIENTATION, IMREAD_UNCHANGED
  from annotator.uniformer.mmcv.utils import check_file_exist, is_str, mkdir_or_exist
label_desc: Use image node_id 3 for calling a global function with example usage: _pillow2array(img, flag, channel_order) and returns: array