Process Images with Python PIL
Tags: python, productivity
Published: 2020-09-19 13:15:59

---
title: Process Images with Python PIL
published: true
description: Convert, resize, apply a filter to images the easy way without the use of photo editing apps.
tags: #Python #Productivity
cover_image: https://res.cloudinary.com/teri/image/upload/v1634660104/Blog%20poster/process%20images.png
---

Have you ever felt stuck trying to convert, resize, apply a filter to images with the aid of a design tool like Photoshop or Illustrator? With Python, we can achieve this with just basic script written in our favourite code editors or IDEs.

Python is one of the most popular programming languages and it is favoured amongst beginners trying to experiment with their first language. Python provides lots of libraries for image processing but we would focus our attention with Python Imaging Library (PIL). PIL helps to perform basic operations on images like resizing, rotation, creating thumbnails, convert between different file formats etc.

In this tutorial, we are going to assume you have a basic understanding of Python and its syntax to be able to follow. Before we begin the installation of PIL, ensure you have the latest Python installed on your system `python3`.

## Installation

The first step in working with image processing is to install the required library, **Pillow**. We will use `pip` to install the required library from the terminal or command line.

```py
$pip3 install pillow
``` 

That's it. Now we can begin.

## Open, rotate and display and image

The following script loads an image, rotate it 90 degrees, and display an image on the default system image viewer.

```py
from PIL import Image
img = Image.open("bulbasaur.jpg")
img.rotate(90).show()
``` 

## Create thumbnails

The thumbnail method, thumbnail() helps to maintain the aspect ratio of images especially when you want it to scale it down/up proportionally. In between the method, enter the dimension you want.

Something you should take note of is that the thumbnail method doesn't return a new image, it just modifies the current one.

```py
from PIL import Image
img = Image.open("bulbasaur.jpg")
img.thumbnail((400, 250))
img.show()
```

## Apply filter to images.

Here we would sharpen, blur, and smoothen an image based on our preference. With this process, we have to import another module, ImageFilter to the pre-existing Image module used to represent a PIL image. 

Replace BLUR attribute with SHARPEN and SMOOTH to see the changes to the processed image.

```py
from PIL import Image, ImageFilter
img = Image.open("bulbasaur.jpg")
filtered_img = img.filter(ImageFilter.BLUR) 
filtered_img.show()
```

## Convert and save 

We can change the format of an image from one form to another.

You can either create a new folder or save the image in the same folder where you want to apply the save() method to.

```py
from PIL import Image
img = Image.open("bulbasaur.jpg")
img.save("/images/processed/new_bulbasaur.png", "png")
```

## Convert image to grayscale

We would be making use of the convert() method to apply changes to an image from its original coloured image to grey.

```py
from PIL import Image
img = Image.open("bulbasaur.jpg")
filtered_img = img.convert("L")
filtered_img.show()
```
<small>The "L" stands for 'luminous'</small>

## Resize image

With this method, resize(), you can resize your image with the following script.

```py
from PIL import Image
img = Image.open("bulbasaur.jpg")
resize = img.resize((300, 300))
resize.show()
```

## Crop image

The crop() method returns a rectangular region from the cropped image. The box is a tuple of the crop rectangle which takes in four input as coordinates, (left, upper, right, lower) represented as figures.

```py
from PIL import Image
img = Image.open("bulbasaur.jpg")
box = (100, 100, 250, 250)
region = img.crop(box)
region.show()
```

## Read the image

You can read the image through its various attributes such as mode, size, and format. We can use the Python Print() function.

Replace the `size` attribute with `filter` and `mode` attributes.

```py
from PIL import Image
img = Image.open("bulbasaur.jpg")
print(img.size)
``` 

## Conclusion
That's it from my end! Remember you can do more exciting things with reading the Pillow documentation and we have barely scratched the surface of endless possibilities we can achieve using this Python library.

If this helped you in any way, kindly leave a feedback or comment below.

## Further reading

[Image effects with PIL(Python Image Library)](https://pillow.readthedocs.io/en/stable/reference/ImageFilter.html)

[Essential Pil (Pillow) Image Tutorial (for Machine Learning People)](https://neptune.ai/blog/pil-image-tutorial-for-machine-learning)