def img_to_array(img, data_format=None):
    'Converts a PIL Image instance to a Numpy array.\n\n    # Arguments\n        img: PIL Image instance.\n        data_format: Image data format,\n            either "channels_first" or "channels_last".\n\n    # Returns\n        A 3D Numpy array.\n\n    # Raises\n        ValueError: if invalid `img` or `data_format` is passed.\n    '
    if (data_format is None):
        data_format = K.image_data_format()
    if (data_format not in {'channels_first', 'channels_last'}):
        raise ValueError('Unknown data_format: ', data_format)
    x = np.asarray(img, dtype=K.floatx())
    if (len(x.shape) == 3):
        if (data_format == 'channels_first'):
            x = x.transpose(2, 0, 1)
    elif (len(x.shape) == 2):
        if (data_format == 'channels_first'):
            x = x.reshape((1, x.shape[0], x.shape[1]))
        else:
            x = x.reshape((x.shape[0], x.shape[1], 1))
    else:
        raise ValueError('Unsupported image shape: ', x.shape)
    return x
Converts a PIL Image instance to a Numpy array.

# Arguments
    img: PIL Image instance.
    data_format: Image data format,
        either "channels_first" or "channels_last".

# Returns
    A 3D Numpy array.

# Raises
    ValueError: if invalid `img` or `data_format` is passed.
def img_to_array(img, data_format=None):
    'Converts a PIL Image instance to a Numpy array.\n\n    # Arguments\n        img: PIL Image instance.\n        data_format: Image data format,\n            either "channels_first" or "channels_last".\n\n    # Returns\n        A 3D Numpy array.\n\n    # Raises\n        ValueError: if invalid `img` or `data_format` is passed.\n    '
    if (data_format is None):
        data_format = K.image_data_format()
    if (data_format not in {'channels_first', 'channels_last'}):
        raise ValueError('Unknown data_format: ', data_format)
    x = np.asarray(img, dtype=K.floatx())
    if (len(x.shape) == 3):
        if (data_format == 'channels_first'):
            x = x.transpose(2, 0, 1)
    elif (len(x.shape) == 2):
        if (data_format == 'channels_first'):
            x = x.reshape((1, x.shape[0], x.shape[1]))
        else:
            x = x.reshape((x.shape[0], x.shape[1], 1))
    else:
        raise ValueError('Unsupported image shape: ', x.shape)
    return x