Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy.array vs img_to_array

What is the difference between numpy.array(image) and img_to_array(image) function? img_to_array is inside keras.preprocessing.image package. I wanted to use it with an image as input to this function.

like image 893
Nishant Avatar asked Dec 11 '18 06:12

Nishant


People also ask

What is Img_to_array?

img_to_array functionConverts a PIL Image instance to a Numpy array. Usage: from PIL import Image img_data = np.

How do you read an image in an array in Python?

imread() function is used to load the image and It also reads the given image (PIL image) in the NumPy array format. Then we need to convert the image color from BGR to RGB. imwrite() is used to save the image in the file.


2 Answers

Well, you can easily find out the answer by looking at the source code of img_to_array:

def img_to_array(img, data_format='channels_last', dtype='float32'):
    """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".
        dtype: Dtype to use for the returned array.
    # Returns
        A 3D Numpy array.
    # Raises
        ValueError: if invalid `img` or `data_format` is passed.
    """
    if data_format not in {'channels_first', 'channels_last'}:
        raise ValueError('Unknown data_format: %s' % data_format)
    # Numpy array x has format (height, width, channel)
    # or (channel, height, width)
    # but original PIL image has format (width, height, channel)
    x = np.asarray(img, dtype=dtype)
    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: %s' % (x.shape,))
    return x

So the main difference is that you can pass a data format argument to img_to_array to put the channels either at the first axis or the last axis. Further, it would ensure that the returned array is a 3D array (for example, if the given input img is a 2D array which might represent a grayscale image, then it would add another axis with dimension 1 to make it a 3D array).

Note that although in the docstring it has been mentioned that the input image is a PIL image instance, however it would also work with the numpy arrays or even Python lists (since the input is first converted to a numpy array: x = np.asarray(img, dtype=dtype)).

like image 179
today Avatar answered Oct 02 '22 10:10

today


As far as I can see on some examples, img_to_array() is a method of an image Class. The Class does not represent an array, but something more abstract, but an image is inherently an array. That might be why you would have a similar result with numpy.array(image).

Note that as methods have more information (call it 'context'), they should be more efficient and reliable. For instance, opencv2 is manipulating BGR images when it comes to representation, and not RGB. It can be confusing at first, but using the proper cv2 library, you don't even have to really think about it (depending of what you intend to do).

like image 43
IMCoins Avatar answered Oct 02 '22 12:10

IMCoins