Python Pillow cropping image
Crop an Image with Python Pillow
Cropping is a key operation in image processing, used to remove unwanted parts of an image or to add desired features. It is widely used in web applications, such as for uploading images.
The crop() function of the Image class in Pillow requires the image to be cropped into a rectangle. The rectangle to be cropped from the image is specified as a four-element tuple, and the cropped rectangle is returned as an image object.
Example
The 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 = Image.open('images/elephant.jpg')
#Display actual image
im.show()
#Left, upper, right, lower
#Crop
cropped = im.crop((1,2,300,300))
#Display the cropped portion
cropped.show()
#Save the cropped image
cropped.save('images/croppedBeach1.jpg')
Output
If you save the above program as Example.py and execute it, it will display the original and cropped images using standard PNG display tools, as shown below.
Original Image
Cropped Image