Image Processing using Pillow library in Python

Rohit Raj
Thrive in AI
Published in
6 min readJul 28, 2022

Python programming language has several libraries for processing images. OpenCV, Pillow/PIL, Scipy and Scikit-Image are the most popular python libraries for processing images. In this tutorial, I will demonstrate basic image processing using the Pillow library.

The colourful images that we see on a computer screen are in reality just two or three-dimensional array of numbers. Black and white images are 2 dimensional arrays where numbers represent intensity of each pixel. Color images are three-dimensional array of numbers where the third dimension contains three numbers, one each for the intensity of red, green, and blue channels. Combination of red, green and blue colors can be used to create any color. Further, there are images with an additional alpha channel which is a measure of the transparency of images. In these images, the third dimension contains four values.

You can install the pillow library by running the following command in your python environment.

pip install Pillow
  1. Reading and Writing Images

We can read images using Image.open function of Pillow library

We can obtain size of image and mode of image using its attributes.

We can save the image using the save method as follows

image.save('test.png')

2. Resize, Crop and Rotation

We can resize the image using resize method of library. We have to provide the required size as an argument to the resize method.

Instead of providing dimensions, we can provide the factor by which the image should be reduced

We can rotate the image using the rotate method. We have to provide the angle of rotation as input.

For cropping the image we have to provide the coordinate of the required area to crop method as follows

The argument to crop is a 4-tuple defining the left, upper, right, and lower pixel coordinate.

We can convert image to grayscale using the convert method

It is a 4 channel so we used “LA” mode for converting to grayscale. For RGB images we have to use “L” mode.

We can split the image into its red, green, and blue channels using the split function

3 Combining image

We can use the paste method to paste one image into another. Its syntax is as below

Image.paste(im, box=None, mask=None)

We have to provide the box argument which is either a 2-tuple giving the upper left corner, a 4-tuple defining the left, upper, right, and lower pixel coordinate, or None (same as (0, 0)). If a 4-tuple is given, the size of the pasted image must match the size of the region.

If we don't provide box argument, it defaults to (0,0) coordinate.

In the above example, I provided box argument so the image was pasted at the desired location.

4. Image Filters

Pillow provides various filters for basic image processing which can be used with Image.filter() method.

The current version of the library provides the following set of predefined image enhancement filters:

BLUR

CONTOUR

DETAIL

EDGE_ENHANCE

EDGE_ENHANCE_MORE

EMBOSS

FIND_EDGES

SHARPEN

SMOOTH

SMOOTH_MORE

Some of the examples of their effects I have given below

5. Screen Grab

We can use screengrab function to take screenshot of screen

6 Writing text on Images

We can use Imagedraw.text() method to write text on images. It takes following major arguments

xy — Coordinates of top left corner of the text.
text — Text to be drawn.
fill — Color to use for the text.
font — An ImageFont instance. Size and font of text can be controlled using this argument

An example of this is given below

7 Drawing Shape on images

Pillow allows us to draw many types of shapes on images. In this tutorial I cover how to draw line and rectangles on images

We can draw rectangles on images using Imagedraw.rectangle() method which takes following arguments

ImageDraw.rectangle(xy, fill=None, outline=None, width=1)

xy — Two points to define the bounding box. Sequence of either [(x0, y0), (x1, y1)] or [x0, y0, x1, y1]. The second point is just outside the drawn rectangle.

outline — Color to use for the outline.

fill — Color to use for the fill.

width — The line width, in pixels.

We can use it as follows

We can draw line on image using Imagedraw.line() method which takes following argument

ImageDraw.line(xy, fill=None, width=0, joint=None)

xy — Sequence of either 2-tuples like [(x, y), (x, y), …] or numeric values like [x, y, x, y, …].

fill — Color to use for the line.

width –The line width, in pixels.

We can use it as follows

8 Selectively Editing images

We can combine our knowledge to selectively edit images i.e apply different filters or transformations to different sections of images.

There are two options. First we can use numpy to filter and transform image.

Here I use numpy to change background of image to yellow color

I first converted the image into a numpy array. Then I used numpy where function to change image pixel values to (255, 255,0,255) which were (0,0,0,0) before.

Next I will use pillow to split, transform and combine image.

First I split image into left and right hand side.

Then I apply Emboss filter to the left side and convert the right side into grayscale. We combine both sides using the paste function of pillow

If you liked my article, Please like and subscribe to my channel.

Source

https://pillow.readthedocs.io/en/stable/reference/index.html

--

--

Rohit Raj
Thrive in AI

Studied at IIT Madras and IIM Indore. Love Data Science