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.
- 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
One can easily check the version of installed Pillow by using the below code
Installed Pillow Version: 7.2.0
Loading the images via Pillow Library
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.
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 a few of them in detail.
Using NumPy module
Numpy module in itself provides various methods to do the same. These methods are –
Method 1: Using asarray() function
asarray() function is used to convert PIL images into NumPy arrays. This function converts the input to an array
<class 'numpy.ndarray'> (200, 400, 3)
Method 2: Using numpy.array() function
By using numpy.array() function which takes an image as the argument and converts to NumPy array
(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.
[[[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
<class 'numpy.ndarray'> (200, 400, 3) <class 'PIL.Image.Image'> RGB (400, 200)
Converting Images using Keras API
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
<class 'PIL.PngImagePlugin.PngImageFile'> PNG RGB (400, 200)
Using Keras API, convert images to Numpy Array and reverting the image from Numpy Array
<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
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.
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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course