code: def ndarray_to_pil(arr, format_str=None):
      """Export an ndarray to a PIL object.
  
      Parameters
      ----------
      Refer to ``imsave``.
  
      """
      if arr.ndim == 3:
          arr = img_as_ubyte(arr)
          mode = {3: "RGB", 4: "RGBA"}[arr.shape[2]]
  
      elif format_str in ["png", "PNG"]:
          mode = "I;16"
          mode_base = "I"
  
          if arr.dtype.kind == "f":
              arr = img_as_uint(arr)
  
          elif arr.max() < 256 and arr.min() >= 0:
              arr = arr.astype(np.uint8)
              mode = mode_base = "L"
  
          else:
              arr = img_as_uint(arr)
  
      else:
          arr = img_as_ubyte(arr)
          mode = "L"
          mode_base = "L"
  
      try:
          array_buffer = arr.tobytes()
      except AttributeError:
          array_buffer = arr.tostring()  # Numpy < 1.9
  
      if arr.ndim == 2:
          im = Image.new(mode_base, arr.T.shape)
          try:
              im.frombytes(array_buffer, "raw", mode)
          except AttributeError:
              im.fromstring(array_buffer, "raw", mode)  # PIL 1.1.7
      else:
          image_shape = (arr.shape[1], arr.shape[0])
          try:
              im = Image.frombytes(mode, image_shape, array_buffer)
          except AttributeError:
              im = Image.fromstring(
                  mode, image_shape, array_buffer
              )  # PIL 1.1.7
      return im
  
file_path: scikit-image/skimage/io/_plugins/pil_plugin.py
imports: import numpy
  from PIL import Image
  from ...util import img_as_ubyte, img_as_uint
label_desc: Use image node_id 4 for calling a global function with example usage: ndarray_to_pil(arr, format_str) and returns: im