English 中文(简体)
Python Pillow - Cropping an Image
  • 时间:2024-11-03

Python Pillow - Cropping an Image


Previous Page Next Page  

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 apppcations, 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.

Example

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

#Import required Image pbrary
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, lowe
#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 displays the original and cropped images using standard PNG display utipty, as follows −

Original image Original Image5

Cropped image Cropped Image

Advertisements