In machine learning, Python uses image data in the form of a NumPy array, i.e., [Height, Width, Channel] format. To enhance the performance of the predictive model, we must know how to load and manipulate images. In Python, we can perform one task in different ways. We have options from Numpy to Pytorch and CUDA, depending on the complexity of the problem.
By the end of this tutorial, you will have hands-on experience with:
Pillow is a preferred image manipulation tool. Python version 2 used Python Image Library (PIL), and Python version 3 uses Pillow Python Library, an upgrade of PIL.
You should first create a virtual environment in Anaconda for different projects. Make sure you have supporting packages like NumPy, SciPy, and Matplotlib to install in the virtual environment you create.
Once you set up the packages, you can easily install Pillow using pip
.
1pip install Pillow
You can confirm that the library is installed correctly by checking its version.
1# check Pillow version number
2import PIL
3print('Pillow Version:', PIL.__version__)
1Pillow Version: 7.0.0
Great! The latest version is now downloaded. Let's move on to the next step.
Here we will learn two ways to load and get the details of an image: use Pillow library and using Matplotlib library.
Select a test image to load and work with Pillow (PIL) library. Images can be either PNG or JPEG. In this example, we'll use an image named kolala.jpeg. Either upload the image in the working directory or give your desired path. The Image
class is the heart of PIL
, and its properties help manipulate the pixels, format, and contrast of the image.
The Image
class uses these functions:*
open()
: This can directly load the image. It has info
properties like format
, which gives information about the digital file format of an image, mode which gives a piece of information about pixel format (e.g., RGB or L), and
size`, which displays the dimensions of the image in pixels (e.g., 480x240).
show()
: This will display the image. Your default photo preview application will pop up.1# load and show an image with Pillow
2from PIL import Image
3# Open the image form working directory
4image = Image.open('kolala.jpeg')
5# summarize some details about the image
6print(image.format)
7print(image.size)
8print(image.mode)
9# show the image
10load_image.show()
1JPEG
2(800, 450)
3RGB
We will use the Matplotlib library to load the same image and display it in the Matplotlib frame. Just like PIL, it has the image
class that performs the same function. Functions used in this piece of code are imread()
, which loads the image in the form of an array of the pixel and imshow()
, which displays that image.
We will use the pyplot
class from the Matplotlib library to plot the image into the frame.
1# load and display an image with Matplotlib
2from matplotlib import image
3from matplotlib import pyplot
4# load image as pixel array
5image = image.imread('kolala.jpeg')
6# summarize shape of the pixel array
7print(image.dtype)
8print(image.shape)
9# display the array of pixels as an image
10pyplot.imshow(image)
11pyplot.show()
After the first step of loading the image using the dtype
argument, we get a report on the data type of the array. In this case, it is 8-bit unsigned integers. The shape of the array is 800 pixels wide by 450 pixels high and 3
denotes color channels for red, green, and blue.
In Python, Pillow is the most popular and standard library when it comes to working with image data.
NumPy uses the asarray()
class to convert PIL images into NumPy arrays. The np.array
function also produce the same result. The type
function displays the class of an image.
The process can be reversed using the Image.fromarray()
function. This function comes in handy when the manipulation is performed on numpy.ndarray
image data, that we laterwant to save as a PNG or JPEG file.
1from PIL import Image
2from numpy import asarray
3# load the image
4image = Image.open('kolala.jpeg')
5# convert image to numpy array
6data = asarray(image)
7print(type(data))
8# summarize shape
9print(data.shape)
10
11# create Pillow image
12image2 = Image.fromarray(data)
13print(type(image2))
14
15# summarize image details
16print(image2.mode)
17print(image2.size)
print(data)
gives the value of each pixel of the NumPy array image.
1print(data)
Now that we have converted our image into a Numpy array, we might come across a case where we need to do some manipulations on an image before using it into the desired model. In this section, you will be able to build a grayscale converter. You can also resize the array of the pixel image and trim it.
After performing the manipulations, it is important to save the image before performing further steps. The format
argument saves the file in different formats, such as PNG, GIF, or PEG.
For example, the code below loads the photograph in JPEG format and saves it in PNG format.
1import numpy as np
2from PIL import Image
3
4im = np.array(Image.open('kolala.jpeg').convert('L')) #you can pass multiple arguments in single line
5print(type(im))
6
7gr_im= Image.fromarray(im).save('gr_kolala.png')
1<class 'numpy.ndarray'>
1load_img_rz = np.array(Image.open('kolala.jpeg').resize((200,200)))
2Image.fromarray(load_img_rz).save('r_kolala.jpeg')
3print("After resizing:",load_img_rz.shape)
1After resizing: (200, 200, 3)
1im = np.array(Image.open('kolala.jpeg'))
2
3print("Before trimming:",im.shape)
4
5im_trim = im[128:384, 128:384]
6print("After trimming:",im_trim.shape)
7
8Image.fromarray(im_trim).save('trim_kolala.jpeg')
Check for the images in the path you have mentioned.
Let's consider the same test image. Keras provides the functions for loading, converting, and saving image data. To install Keras API in an Anaconda virtual environment, use the conda install -c anaconda keras
command (CPU version). Keras runs on the top of the TensorFlow framework. Make sure the package is correctly installed.
These functions can be useful convenience functions when getting started on a new dee- learning computer vision project or when you need to inspect specific images.
Keras provides the load_img
function for loading a PIL image. Learn more about the function here
.
1from keras.preprocessing.image import load_img
2import warnings
3
4# load the image
5img = load_img('kolala.jpeg')
6# report details about the image
7print(type(img))
8print(img.format)
9print(img.mode)
10print(img.size)
11# show the image
12img.show()
According to the output below, we can confirm that the loaded image is in PIL format and has JPEG format RGB color channels and a size of 800 x 450 pixels. The image will be displayed in your default image viewer.
Keras uses the img_to_array
function to convert the PIL image into numpy. The API also provides the array_to_img()
function, which can be used for converting an array of pixel data into a PIL image.
1# example of converting an image with the Keras API
2from keras.preprocessing.image import load_img
3from keras.preprocessing.image import img_to_array
4from keras.preprocessing.image import array_to_img
5
6# load the image
7img = load_img('kolala.jpeg')
8print("Orignal:" ,type(img))
9
10# convert to numpy array
11img_array = img_to_array(img)
12print("NumPy array info:")
13print(type(img_array))
14
15print("type:",img_array.dtype)
16print("shape:",img_array.shape)
17# convert back to image
18
19img_pil = array_to_img(img_array)
20print("converting NumPy array:",type(img_pil))
The Keras API uses the save_img()
function to save an image locally. Here the function takes the path and the file name where we want to save the image data in NumPy array format. This function is useful when you have manipulated the image and wish to save the image for later use.
1# example of saving an image with the Keras API
2from keras.preprocessing.image import load_img
3from keras.preprocessing.image import save_img
4# save the image with a new filename
5save_img('Keras_kolala.png', img_array)
6# load the image to confirm it was saved correctly
7img = load_img('Keras_kolala.png')
8print(type(img))
9print(img.format)
10print(img.mode)
11print(img.size)
12img.show()
OpenCV is a library that performs traditional computer vision algorithms. The Tensorflow + Keras framework is a go-to option for anyone who wants to work with deep learning. OpenCV version 3.x has introduced DNN and Caffe frameworks to solve deep learning problems .
To work with an OpenCV library, you need to install it in the virtual environment using pip install opencv-contrib-python
. The cv2
package provides an imread()
function to load the image. It also reads a PIL image in the NumPy array format. The only thing we need to convert is the image color from BGR to RGB. imwrite()
saves the image in the file.
1import cv2
2
3im = cv2.imread('kolala.jpeg')
4img = cv2.cvtColor(im, cv2.COLOR_BGR2RGB) # BGR -> RGB
5cv2.imwrite('opncv_kolala.png', img)
6print (type(img))
1<class 'numpy.ndarray'>
Python is a flexible tool, giving us a choice to load a PIL image in two different ways. In this guide, you learned some manipulation tricks on a Numpy Array image, then converted it back to a PIL image and saved our work. This guide also gave you a heads up on converting images into an array form by using Keras API and OpenCV library. Further, you can follow the Pillow library documentation link and try performing different manipulation techniques, such as building a function to expand the image data and feed into deep learning neural networks.
Feel free to contact me with any questions at Codealphabet.
Check out these Python courses from Pluralsight to continue learning!