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. Show
By the end of this tutorial, you will have hands-on experience with:
Pillow LibraryPillow 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
shell You can confirm that the library is installed correctly by checking its version.
python
Great! The latest version is now downloaded. Let's move on to the next step. Loading the ImageHere we will learn two ways to load and get the details of an image: use Pillow library and using Matplotlib library. Method 1: Pillow LibrarySelect 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 The
python
Method 2: Matplotlib libraryWe will use the Matplotlib library to load the same image and display it in the Matplotlib frame. Just like PIL, it has the We will use the
python After the first step of loading the image using the Convert to NumPy Array and BackIn Python, Pillow is the most popular and standard library when it comes to working with image data. NumPy uses the The process can be reversed using the
python
python Manipulating and Saving the ImageNow 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 For example, the code below loads the photograph in JPEG format and saves it in PNG format. Converting Images to Grayscale
python
Resizing the Image 0python 1Trimming the Image 2python Check for the images in the path you have mentioned. Keras APILet'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 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. Loading an Image With Keras APIKeras provides the 3python 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. Converting an Image With Keras APIKeras uses the 4python Saving an Image with KerasThe Keras API uses the 5python OpenCV LibraryOpenCV 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 6python
ConclusionPython 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. |