How to Convert images to NumPy array?

Images are an easier way to represent the working model. In Machine Learning, Python uses the image data in the format of Height, Width, Channel format. i.e. Images are converted into Numpy Array in Height, Width, Channel format.  In this article we will see How to Convert images to NumPy array?

Modules Needed:

• NumPy: By default in higher versions of Python like 3.x onwards, NumPy is available and if not  available(in lower versions), one can install by using

pip install numpy

• Pillow: This has to be explicitly installed in later versions too. It is a preferred image manipulation tool. In Python 3, Pillow python library which is nothing but the upgradation of PIL only. It can be installed using

pip install Pillow

Let us check for an image that is in the PNG or JPEG format. The image can be referred via its path. Image class is the heart of PIL. It has open() function which opens up an image and digital file format can be retrieved as well as pixel format.

Image Used:

Python3

 `from` `PIL ``import` `Image``image ``=` `Image.``open``(``'Sample.png'``)` `# summarize some details about the image``print``(image.``format``)``print``(image.size)``print``(image.mode)`

Output :

```PNG
(400, 200)
RGB```

Converting an image into NumPy Array

Python provides many modules and API’s for converting an image into a NumPy array. Let’s discuss to Convert images to NumPy array in Python.

Using NumPy module to Convert images to NumPy array

Numpy module in itself provides various methods to do the same. These methods are –

Example 1:Using asarray() function

asarray() function is used to convert PIL images into NumPy arrays. This function converts the input to an array

Python3

 `# Import the necessary libraries``from` `PIL ``import` `Image``from` `numpy ``import` `asarray`  `# load the image and convert into``# numpy array``img ``=` `Image.``open``(``'Sample.png'``)` `# asarray() class is used to convert``# PIL images into NumPy arrays``numpydata ``=` `asarray(img)` `# ``print``(``type``(numpydata))` `#  shape``print``(numpydata.shape)`

Output :

```<class 'numpy.ndarray'>
(200, 400, 3)```

Example 2: Using numpy.array() function

By using numpy.array() function which takes an image as the argument and converts to NumPy array

Python3

 `from` `PIL ``import` `Image``import` `numpy`  `img``=` `Image.``open``(``"Sample.png"``)``np_img ``=` `numpy.array(img)` `print``(np_img.shape)`

Output :

`(200, 400, 3)`

In order to get the value of each pixel of the NumPy array image, we need to print the retrieved data that got either from asarray() function or array() function.

Python3

 `# Import the necessary libraries``from` `PIL ``import` `Image``from` `numpy ``import` `asarray`  `# load the image and convert into ``# numpy array``img ``=` `Image.``open``(``'Sample.png'``)``numpydata ``=` `asarray(img)` `# data``print``(numpydata)`

Output :

```[[[111  60   0]
[116  65   0]
[122  69   0]
...
[ 97  47   0]
[ 99  47   0]
[100  49   0]]
[[111  61   0]
[118  65   0]
[122  69   0]
...
[ 97  47   0]
[ 99  48   0]
[100  49   0]]
[[118  65   0]
[122  69   0]
[126  73   3]
...
[ 98  48   0]
[100  49   0]
[100  49   0]]
...
[[ 96  44   7]
[ 95  43   6]
[ 93  41   4]
...
[225  80   3]
[228  80   0]
[229  78   0]]
[[ 93  40   6]
[ 90  37   5]
[ 85  32   0]
...
[226  81   4]
[231  80   1]
[232  79   1]]
[[ 89  36   4]
[ 84  31   0]
[ 79  26   0]
...
[228  81   4]
[232  81   4]
[233  80   2]]]```

Getting back the image from converted Numpy Array

Image.fromarray() function helps to get back the image from converted numpy array. We get back the pixels also same after converting back and forth. Hence, this is very much efficient

Python3

 `img ``=` `Image.``open``(``'Sample.png'``)``numpydata ``=` `asarray(img)` `print``(``type``(numpydata))` `#  shape``print``(numpydata.shape)` `# Below is the way of creating Pillow ``# image from our numpyarray``pilImage ``=` `Image.fromarray(numpydata)``print``(``type``(pilImage))` `# Let us check  image details``print``(pilImage.mode)``print``(pilImage.size)`

Output :

```<class 'numpy.ndarray'>
(200, 400, 3)
<class 'PIL.Image.Image'>
RGB
(400, 200)```

Using Keras API  to Convert images to NumPy array

Keras API provides the functions for loading, converting, and saving image data. Keras is possible to run on the top of the TensorFlow framework and hence that is mandatory to have. Deep learning computer vision images require Keras API. To install it type the below command in the terminal

pip install keras

As Keras requires TensorFlow 2.2 or higher. If not there, need to install it. To install it type the below command in the terminal.

pip install tensorflow

Python3

 `from` `keras.preprocessing.image ``import` `load_img``import` `warnings` `# load the image via load_img ``# function``img ``=` `load_img(``'sample.png'``)` `# details about the image printed below``print``(``type``(img)) ``print``(img.``format``)``print``(img.mode)``print``(img.size)`

Output :

```<class 'PIL.PngImagePlugin.PngImageFile'>
PNG
RGB
(400, 200)```

Using Keras API, convert images to Numpy Array and reverting the image from Numpy Array

Python3

 `from` `keras.preprocessing.image ``import` `img_to_array``from` `keras.preprocessing.image ``import` `array_to_img` `# details about the image printed below``print``(``type``(img))``print``(img.``format``)``print``(img.mode)``print``(img.size)` `# convert the given image into  numpy array``img_numpy_array ``=` `img_to_array(img)``print``(``"Image is converted and NumPy array information :"``)` `# ``print``(``type``(img_numpy_array))` `# type: float32``print``(``"type:"``, img_numpy_array.dtype)` `# shape: (200, 400, 3)``print``(``"shape:"``, img_numpy_array.shape)` `# convert back to image``img_pil_from_numpy_array ``=` `array_to_img(img_numpy_array)` `# ``print``(``"converting NumPy array into image:"``,``      ``type``(img_pil_from_numpy_array))`

Output :

```<class 'PIL.PngImagePlugin.PngImageFile'>
PNG
RGB
(400, 200)
Image is converted and NumPy array information :
<class 'numpy.ndarray'>
type: float32
shape: (200, 400, 3)
converting NumPy array into image: <class 'PIL.Image.Image'>```

From the above output, we can check that the source image PIL.Image.Image and destination image types are the same.

Using OpenCV Library to Convert images to NumPy array

OpenCV version from 3.x has DNN and Caffe frameworks, and they are very helpful to solve deep learning problems. It can be installed by using

pip install opencv-contrib-python

cv2 package has the following methods

• 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.

Python3

 `import` `cv2` `image ``=` `cv2.imread(``'Sample.png'``)` `# BGR -> RGB``img ``=` `cv2.cvtColor(image, cv2.COLOR_BGR2RGB)` `cv2.imwrite(``'opncv_sample.png'``, img) ``print` `(``type``(img))`

Output :

`<class 'numpy.ndarray'>`

Conclusion

Python is a very flexible tool and we have seen ways of converting images into Numpy Array and similarly back to images using different APIs. Manipulating the converted array and forming different image data and one can feed into deep learning neural networks.

Previous
Next