Python Pillow - Cropping an Image


Cropping is one of the important operations of the image processing to remove unwanted portions of an image as well as to add required features to an image. It is widely used process in web applications, for uploading an image.

The crop() function of the image class in Pillow requires the portion to be cropped as rectangle. The rectangle portion to be cropped from an image is specified as a four-element tuple and returns the rectangle portion of the image that has been cropped as an image Object.


Following example demonstrates how to rotate an image using python pillow −

#Import required Image library
from PIL import Image

#Create an Image Object from an Image
im ='images/elephant.jpg')

#Display actual image

#left, upper, right, lowe
cropped = im.crop((1,2,300,300))

#Display the cropped portion

#Save the cropped image'images/croppedBeach1.jpg')


If you save the above program as and execute, it displays the original and cropped images using standard PNG display utility, as follows −

Original image Original Image5

Cropped image Cropped Image