code: def array_to_img(
      x, data_format="channels_last", scale=True, dtype="float32"
  ):
      """Converts a 3D Numpy array to a PIL Image instance.
  
      # Arguments
          x: Input Numpy array.
          data_format: Image data format, either "channels_first" or "channels_last".
              Default: "channels_last".
          scale: Whether to rescale the image such that minimum and maximum values
              are 0 and 255 respectively.
              Default: True.
          dtype: Dtype to use.
              Default: "float32".
  
      # Returns
          A PIL Image instance.
  
      # Raises
          ImportError: if PIL is not available.
          ValueError: if invalid `x` or `data_format` is passed.
      """
      if pil_image is None:
          raise ImportError(
              "Could not import PIL.Image. "
              "The use of `array_to_img` requires PIL."
          )
      x = np.asarray(x, dtype=dtype)
      if x.ndim != 3:
          raise ValueError(
              "Expected image array to have rank 3 (single image). "
              "Got array with shape: %s" % (x.shape,)
          )
  
      if data_format not in {"channels_first", "channels_last"}:
          raise ValueError("Invalid data_format: %s" % data_format)
  
      # Original Numpy array x has format (height, width, channel)
      # or (channel, height, width)
      # but target PIL image has format (width, height, channel)
      if data_format == "channels_first":
          x = x.transpose(1, 2, 0)
      if scale:
          x = x - np.min(x)
          x_max = np.max(x)
          if x_max != 0:
              x /= x_max
          x *= 255
      if x.shape[2] == 4:
          # RGBA
          return pil_image.fromarray(x.astype("uint8"), "RGBA")
      elif x.shape[2] == 3:
          # RGB
          return pil_image.fromarray(x.astype("uint8"), "RGB")
      elif x.shape[2] == 1:
          # grayscale
          if np.max(x) > 255:
              # 32-bit signed integer grayscale image. PIL mode "I"
              return pil_image.fromarray(
                  x[:, :, 0].astype("int32"), "I"
              )
          return pil_image.fromarray(x[:, :, 0].astype("uint8"), "L")
      else:
          raise ValueError(
              "Unsupported channel number: %s" % (x.shape[2],)
          )
  
file_path: keras-preprocessing/keras_preprocessing/image/utils.py
imports: import io
  import os
  import warnings
  from pathlib import Path
  import numpy
label_desc: Use image node_id 7 for calling a global function with example usage: array_to_img(x, data_format, scale, dtype) and returns: pil_image, pil_image, pil_image, pil_image