Pillow In Python


Python has various libraries for manipulating Images, but Pillow library stands out as it provides major functionalities, works on almost every operating system, supports every image format like ‘jpeg’, ‘png’, ‘gif’ etc. Pillow is written on top of the “Python Imaging Library” (PIL). The article focuses on exploring Pillow library functions for image processing like Rotating an Image, Blurring an Image, Cropping, Creating thumbnails, and writing Text on Images 


Installation of Pillow can be done using ‘pip install pillow’, the main module to handle images in Pillow is “Image” which can be imported from PIL package. Let’s open an image programmatically using the pillow.

#show image

from PIL import Image
image = Image.open("Image.jpg")

This image will display using the ‘show’ method.

The major attributes provided by the library are ‘filename’, ‘mode’, ‘height’, ‘width’, ‘size’ etc.

from PIL import Image
image = Image.open("C:\\Books\\Image.jpg")
print("mode:", image.mode)
print("size:", image.size)
print("filename:", image.filename)
print("format:", image.format)
print("height:", image.height)
print("width:", image.width)

Rotating an Image

Image can be rotated using the ‘rotate’ function of Image by specifying the degree of rotation.

# rotate 

from PIL import Image
image = Image.open("C:\\Books\\Image.jpg")
image = image.rotate(80)

Blurring an Image

Blurring can be done by reducing the quality of the Image through a Filter, the ImageFilter class has various filtering options like Simple Blur, Box Blur, Gaussian Blur which can be supplied to the ‘filter()’ method.

# Blur

from PIL import Image, ImageFilter
image = Image.open("C:\\Books\\Image.jpg")
image = image.filter(ImageFilter.BLUR)

Cropping an Image

Images can be cropped using the ‘crop’ function

# Cropping

from PIL import Image, ImageFilter
image = Image.open("C:\\Books\\Image.jpg")
cropped_image = image.crop((10,10,200,200))


Creating Thumbnail

For creating a thumbnail of an image thumbnail() function should be used, thumbnail function expects the height/width of the thumbnail we want to create


from PIL import Image
image = Image.open('C:\\Books\\Image.jpg')
thumbnail = Image.open('C:\\Books\\Thumbnail_Image.jpg')

Text on Image

For writing the text on the image, we need to pass the coordinates where to display the text, the color of the text, and the text itself.

from PIL import Image, ImageDraw

img = Image.open('C:\\Books\\Image.jpg')
draw = ImageDraw.Draw(img)
draw.text((20, 96), "Kubernetes!", fill=(255, 155, 0))


In the article we have seen the basic operations we can perform on images using the Pillow library, it’s extremely easy to manipulate images using Pillow, although we have seen 5 major operations using Pillow, a lot more functionalities will be provided by Pillow.