Python Pillow (PIL) Tutorial – With Examples

Pillow

Pillow is a Python Imaging Library (called as PIL), which supports opening, manipulating (filtering, enhancing, masking, watermarking, resizing etc) and saving of images.

The latest version can identify and read more than 30 image formats.

Write support is intentionally restricted to few image formats.

Examples

Show or display the image

Below example code reads an image on PC and displays it on the default image viewer (mine is Windows 10 PC with default image viewer as Photos application. So, when i run below code, image gets displayed on Photos app.

from PIL import Image

im = Image.open("C:\\Users\\Srini\\mobile-port-device-frame-example-google-chrome.jpg")
im.show()

In the above code, first we import Image class from PIL module

from PIL import Image

Image.open() method reads the image and returns image object on successful read. Note that if you want to open an image from your PC, use ‘\\’ instead of ‘\’ in the file path.

im = Image.open("C:\\Users\\Srini\\mobile-port-device-frame-example-google-chrome.jpg")

show() method opens the default image viewer and displays the image

im.show()

Resizing image with PIL

Also see Resize Image keeping aspect ratio – Python PIL Image Module

Example python script to resize image using PIL

from PIL import Image

im = Image.open("C:\\Users\\Srini\\Desktop\\_Archive\\blog_ghs\\mobile-port-device-frame-example-google-chrome.jpg")
height = 900 #modify it as per your need
width = 900 #modify it as per your need
im1 = im.resize((width, height), Image.ANTIALIAS)
im2 = im1.save("C:\\Users\\Srini\\Desktop\\_Archive\\blog_ghs\\mobile-port-device-frame-example-google-chrome_resized.jpg")

First import Image class from PIL module

from PIL import Image

Image.open() method reads the image

im = Image.open("C:\\Users\\Srini\\Desktop\\_Archive\\blog_ghs\\mobile-port-device-frame-example-google-chrome.jpg")

Define height and width dimensions of the image you want to resize to.

height = 900 #modify it as per your need
width = 900 #modify it as per your need

resize() method resizes the image to the mentioned image height and width

im1 = im.resize((width, height), Image.ANTIALIAS)

save() method saves the image to the mentioned path. Keep same file name if you want to replace the old image with resized image. Change the name (like in my example code) to create a new resized image.

im2 = im1.save("C:\\Users\\Srini\\Desktop\\_Archive\\blog_ghs\\mobile-port-device-frame-example-google-chrome_resized.jpg")

Also check Top Rated Python Books on Amazon 2020

Convert image with Pillow

Python code to convert an image to other format (for example, from jpg to png)

from PIL import Image

im = Image.open("C:\\Users\\Srini\\Desktop\\_Archive\\blog_ghs\\mobile-port-device-frame-example-google-chrome.jpg")
im2 = im.save("C:\\Users\\Srini\\Desktop\\_Archive\\blog_ghs\\mobile-port-device-frame-example-google-chrome_converted.png",'png')

Above code imports the Image class from PIL library

from PIL import Image

Image.open() reads the image

im = Image.open("C:\\Users\\Srini\\Desktop\\_Archive\\blog_ghs\\mobile-port-device-frame-example-google-chrome.jpg")

The second parameter in save() method specifies the new format of the file. Also, you have to change the image file extension to new format (‘png’ in my example)

im2 = im.save("C:\\Users\\Srini\\Desktop\\_Archive\\blog_ghs\\mobile-port-device-frame-example-google-chrome_converted.png",'png')

Cropping image in python

Below program crops an image using PIL library

from PIL import Image

im = Image.open("C:\\Users\\Srini\\Desktop\\_Archive\\blog_ghs\\mobile-port-device-frame-example-google-chrome.jpg")
im1 = im.crop((100, 100, 250, 250))
im2 = im1.save("C:\\Users\\Srini\\Desktop\\_Archive\\blog_ghs\\mobile-port-device-frame-example-google-chrome_cropped.jpg")

crop() method returns cropped copy of the image. It takes a 4 tuple which defines left, upper, right and lower pixel coordinates.

im1 = im.crop((100, 100, 250, 250))

Original Image:

Cropped Image:

Rotating image with PIL

Below code rotates the image in python using PIL

from PIL import Image

im = Image.open("C:\\Users\\srini\\Desktop\\_Archive\\blog_ghs\\mobile-port-device-frame-example-google-chrome.jpg")
im1 = im.rotate(180)
im2 = im1.save("C:\\Users\\srini\\Desktop\\_Archive\\blog_ghs\\mobile-port-device-frame-example-google-chrome_rotated.jpg")

rotate() method creates a rotated copy of the image. The angle value is in degrees.

References

https://en.wikipedia.org/wiki/Python_Imaging_Library

https://pypi.org/project/Pillow/

About the Author

SRINI S

A passionate blogger. Love to share solutions and best practices on wordpress hosting, issues and fixes, excel VBA macros and other apps

Leave a Reply

Your email address will not be published. Required fields are marked *