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 install Pillow
You can confirm that the library is installed correctly by checking its version.
1 2 3
# check Pillow version number import PIL print('Pillow Version:', PIL.__version__)
Pillow 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.
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), andsize`, 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 2 3 4 5 6 7 8 9 10
# load and show an image with Pillow from PIL import Image # Open the image form working directory image = Image.open('kolala.jpeg') # summarize some details about the image print(image.format) print(image.size) print(image.mode) # show the image load_image.show()
1 2 3
JPEG (800, 450) RGB
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 2 3 4 5 6 7 8 9 10 11
# load and display an image with Matplotlib from matplotlib import image from matplotlib import pyplot # load image as pixel array image = image.imread('kolala.jpeg') # summarize shape of the pixel array print(image.dtype) print(image.shape) # display the array of pixels as an image pyplot.imshow(image) pyplot.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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
from PIL import Image from numpy import asarray # load the image image = Image.open('kolala.jpeg') # convert image to numpy array data = asarray(image) print(type(data)) # summarize shape print(data.shape) # create Pillow image image2 = Image.fromarray(data) print(type(image2)) # summarize image details print(image2.mode) print(image2.size)
print(data) gives the value of each pixel of the NumPy array image.
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.
1 2 3 4 5 6 7
import numpy as np from PIL import Image im = np.array(Image.open('kolala.jpeg').convert('L')) #you can pass multiple arguments in single line print(type(im)) gr_im= Image.fromarray(im).save('gr_kolala.png')
1 2 3
load_img_rz = np.array(Image.open('kolala.jpeg').resize((200,200))) Image.fromarray(load_img_rz).save('r_kolala.jpeg') print("After resizing:",load_img_rz.shape)
After resizing: (200, 200, 3)
1 2 3 4 5 6 7 8
im = np.array(Image.open('kolala.jpeg')) print("Before trimming:",im.shape) im_trim = im[128:384, 128:384] print("After trimming:",im_trim.shape) Image.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
1 2 3 4 5 6 7 8 9 10 11 12
from keras.preprocessing.image import load_img import warnings # load the image img = load_img('kolala.jpeg') # report details about the image print(type(img)) print(img.format) print(img.mode) print(img.size) # show the image img.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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# example of converting an image with the Keras API from keras.preprocessing.image import load_img from keras.preprocessing.image import img_to_array from keras.preprocessing.image import array_to_img # load the image img = load_img('kolala.jpeg') print("Orignal:" ,type(img)) # convert to numpy array img_array = img_to_array(img) print("NumPy array info:") print(type(img_array)) print("type:",img_array.dtype) print("shape:",img_array.shape) # convert back to image img_pil = array_to_img(img_array) print("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 2 3 4 5 6 7 8 9 10 11 12
# example of saving an image with the Keras API from keras.preprocessing.image import load_img from keras.preprocessing.image import save_img # save the image with a new filename save_img('Keras_kolala.png', img_array) # load the image to confirm it was saved correctly img = load_img('Keras_kolala.png') print(type(img)) print(img.format) print(img.mode) print(img.size) img.show()
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.
1 2 3 4 5 6
import cv2 im = cv2.imread('kolala.jpeg') img = cv2.cvtColor(im, cv2.COLOR_BGR2RGB) # BGR -> RGB cv2.imwrite('opncv_kolala.png', img) print (type(img))
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.