def _get_image_type_from_array(arr):
  """Find image type based on array dimensions.

  Raises error on invalid image dimensions.
  Args:
    arr: numpy array. Input array.

  Returns:
    str. "RGB" or "L", meant for PIL.Image.fromarray.
  """
  if len(arr.shape) == 3 and arr.shape[2] == 3:
    # 8-bit x 3 colors
    return 'RGB'
  elif len(arr.shape) == 2:
    # 8-bit, gray-scale
    return 'L'
  else:
    raise ValueError(
        'Input array must have either 2 dimensions or 3 dimensions where the '
        'third dimension has 3 channels. i.e. arr.shape is (x,y) or (x,y,3). '
        'Found shape {}.'.format(arr.shape))