First project in Image Processing

August 13th is the international day of left-handedness and somehow I see myself represented since I always tell my wife to look to the left and then she looks to the right,so I tell her to look to the other left. It seems that she has two left hands, and that is not ambidextrous, so in honor of left-handed people today I line up the whole article on the left, so you can see how comfortable our life is.

Nedward «Ned» Flanders ……”The Leftorium”

What is Pillow Library

Image processing is performing operations on an image to enhance it or to extract some useful information and to know about the attributes of the image. A systematic study of the image to find out some useful information about that image is known as image processing.

Python allows image processing using different libraries and one of them is Pillow, which is an open-source Python Imaging Library that adds image processing capabilities to your Python interpreter.

Pillow offers all types of capabilities like image transformation, rotation, resizing, statistics of the image, etc. It supports the extensive file format and is designed to fast access the data stored in pixels.

In this article, we explore Pillow and learn about its image processing capabilities.

Implementation:

Like any other library, we will first install pillow using pip install pillow

Importing required library

We will start by importing Image function from PIL. This function is used to process the image and perform operations on it.

from PIL import Image

Loading the image

We can work on any image of our choice of any format. I am using an image of a bird that I downloaded from unsplash.com.

Thanks to Artem for sharing their work on Unsplash.

img = Image.open("left.jpg")

img
png

Image processing

Lets us start the image processing by knowing the mode, size, and the format of the image.

Basic Properties

Pillow has inbuilt functions to know the basic properties of the image. Lets us see how we use them to draw some information.

print('Format:',img.format)

print('Size',img.size)

print('Mode:', img.mode)
Format: JPEG
Size (640, 960)
Mode: RGB

Cropping the Image

In the cropping section, we will crop the image by creating a box of a particular size and cut it out of the image.

box1 = (200, 200, 800, 800)

crop1 = img.crop(box1)

crop1
png

As you can see according to the size of the box we have a cut out from the image which is the cropped version.

Splitting the Image

As we already know from above that the image is in RGB mode, in this step we will split it into ‘L’ mode i.e. a single channel Luminance.

R, G, B = img.split()

R

The image is split into three parts with a single channel ‘L’.

Transformations

Let us analyze different transformation functions. Starting with resizing the image.

Resizingresized_image = img.resize((256, 256))

resized_image
png
Rotating Imagerotated_image = img.rotate(90)

rotated_image
png
Transposing Imagetran_image = img.transpose(Image.FLIP_TOP_BOTTOM)

tran_image
png
Changing colorimg1 = img.convert('1')
img1
png

Filters

Let us analyze different filters that are defined under the Pillow library.

Blur Imagefrom PIL import ImageFilter

blurred = img.filter(ImageFilter.BLUR)

blurred
png
Sharpen Imagesharped= img.filter(ImageFilter.BLUR)

sharped
png
Finding Edgesedges = sharped.filter(ImageFilter.FIND_EDGES)

edges
png
Enhancing Edgesen_edges = sharped.filter(ImageFilter.EDGE_ENHANCE)

en_edges
png
Emboss Filteremb = sharped.filter(ImageFilter.EMBOSS)

emb
png

This is just an introduction to the image processing capabilities of the Pillow. All these functions are helpful in the initial stage of image processing. There are many advanced functions that can be used at a later stage.

Conclusion:

In this article, we have seen the basic image processing capabilities of the Pillow. We saw how we can access different image attributes, how we can perform different operations like transpose, resize, etc. This is just an introduction to what all Pillow can do.

I hope it will help you to develop your training.

No matter what books or blogs or courses or videos one learns from, when it comes to implementation everything might look like “Out of Syllabus”

Best way to learn is by doing!
Best way to learn is by teaching what you have learned!

Never give up!

See you in Linkedin!

Master in Data Science. Passionate about learning new skills. Former branch risk analyst. https://www.linkedin.com/in/oscar-rojo-martin/. www.oscarrojo.es

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store