DEV Community

Cover image for Process Images with Python PIL
teri
teri

Posted on • Edited on

Process Images with Python PIL

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.

$pip3 install pillow
Enter fullscreen mode Exit fullscreen mode

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.

from PIL import Image
img = Image.open("bulbasaur.jpg")
img.rotate(90).show()
Enter fullscreen mode Exit fullscreen mode

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.

from PIL import Image
img = Image.open("bulbasaur.jpg")
img.thumbnail((400, 250))
img.show()
Enter fullscreen mode Exit fullscreen mode

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.

from PIL import Image, ImageFilter
img = Image.open("bulbasaur.jpg")
filtered_img = img.filter(ImageFilter.BLUR) 
filtered_img.show()
Enter fullscreen mode Exit fullscreen mode

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.

from PIL import Image
img = Image.open("bulbasaur.jpg")
img.save("/images/processed/new_bulbasaur.png", "png")
Enter fullscreen mode Exit fullscreen mode

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.

from PIL import Image
img = Image.open("bulbasaur.jpg")
filtered_img = img.convert("L")
filtered_img.show()
Enter fullscreen mode Exit fullscreen mode

The "L" stands for 'luminous'

Resize image

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

from PIL import Image
img = Image.open("bulbasaur.jpg")
resize = img.resize((300, 300))
resize.show()
Enter fullscreen mode Exit fullscreen mode

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.

from PIL import Image
img = Image.open("bulbasaur.jpg")
box = (100, 100, 250, 250)
region = img.crop(box)
region.show()
Enter fullscreen mode Exit fullscreen mode

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.

from PIL import Image
img = Image.open("bulbasaur.jpg")
print(img.size)
Enter fullscreen mode Exit fullscreen mode

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)

Essential Pil (Pillow) Image Tutorial (for Machine Learning People)

Top comments (4)

Collapse
 
cannuhlar profile image
Can Nuhlar

If you want to get familiar with python it's good but for actual usage; image-magick has a CLI tool that does much more than this. imagemagick.org/script/convert.php

Collapse
 
terieyenike profile image
teri

I would look into it.

Thanks

Collapse
 
titanhero profile image
Lex

Cool very useful, thanks manπŸ˜βœŒοΈπŸ‘

Collapse
 
terieyenike profile image
teri

Glad you found it useful.