def array_to_img(x, data_format=None, scale=True):
    'Converts a 3D Numpy array to a PIL Image instance.\n\n    # Arguments\n        x: Input Numpy array.\n        data_format: Image data format.\n            either "channels_first" or "channels_last".\n        scale: Whether to rescale image values\n            to be within `[0, 255]`.\n\n    # Returns\n        A PIL Image instance.\n\n    # Raises\n        ImportError: if PIL is not available.\n        ValueError: if invalid `x` or `data_format` is passed.\n    '
    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=K.floatx())
    if (x.ndim != 3):
        raise ValueError('Expected image array to have rank 3 (single image). Got array with shape:', x.shape)
    if (data_format is None):
        data_format = K.image_data_format()
    if (data_format not in {'channels_first', 'channels_last'}):
        raise ValueError('Invalid data_format:', data_format)
    if (data_format == 'channels_first'):
        x = x.transpose(1, 2, 0)
    if scale:
        x = (x + max((- np.min(x)), 0))
        x_max = np.max(x)
        if (x_max != 0):
            x /= x_max
        x *= 255
    if (x.shape[2] == 3):
        return pil_image.fromarray(x.astype('uint8'), 'RGB')
    elif (x.shape[2] == 1):
        return pil_image.fromarray(x[:, :, 0].astype('uint8'), 'L')
    else:
        raise ValueError('Unsupported channel number: ', x.shape[2])
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".
    scale: Whether to rescale image values
        to be within `[0, 255]`.

# Returns
    A PIL Image instance.

# Raises
    ImportError: if PIL is not available.
    ValueError: if invalid `x` or `data_format` is passed.
def array_to_img(x, data_format=None, scale=True):
    'Converts a 3D Numpy array to a PIL Image instance.\n\n    # Arguments\n        x: Input Numpy array.\n        data_format: Image data format.\n            either "channels_first" or "channels_last".\n        scale: Whether to rescale image values\n            to be within `[0, 255]`.\n\n    # Returns\n        A PIL Image instance.\n\n    # Raises\n        ImportError: if PIL is not available.\n        ValueError: if invalid `x` or `data_format` is passed.\n    '
    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=K.floatx())
    if (x.ndim != 3):
        raise ValueError('Expected image array to have rank 3 (single image). Got array with shape:', x.shape)
    if (data_format is None):
        data_format = K.image_data_format()
    if (data_format not in {'channels_first', 'channels_last'}):
        raise ValueError('Invalid data_format:', data_format)
    if (data_format == 'channels_first'):
        x = x.transpose(1, 2, 0)
    if scale:
        x = (x + max((- np.min(x)), 0))
        x_max = np.max(x)
        if (x_max != 0):
            x /= x_max
        x *= 255
    if (x.shape[2] == 3):
        return pil_image.fromarray(x.astype('uint8'), 'RGB')
    elif (x.shape[2] == 1):
        return pil_image.fromarray(x[:, :, 0].astype('uint8'), 'L')
    else:
        raise ValueError('Unsupported channel number: ', x.shape[2])