Cropping An Image Using Python And Pillow

Overview:

  • In simple terms, a digital image is represented as a collection of pixels.

 

  • Images are captured from various sources like a digital camera, a document scanner, a mathematical function and so on.

 

  • In Image Processing, often images needs to be edited for better presentation of the final image artifact. Cropping is one of the most common image operations performed to remove unwanted portions of an image as well as to add needed features to an image.

 

Cropping using Pillow:

  • The crop() function of the image class in Pillow-The Python Image Processing library requires the portion to be cropped as rectangle.

   

  • The rectangle portion to be cropped from an image is specified as a four-element tuple.

   

  • The crop() method returns the rectangular portion of the image that has been cropped as an Image Object.

     

Example:

# import the Python Image processing Library

from PIL import Image

 

# Create an Image object from an Image

imageObject  = Image.open("./iceberg.jpg")

 

# Crop the iceberg portion

cropped     = imageObject.crop((100,30,400,300))

 

# Display the cropped portion

cropped.show()

Output:

In the output both the original image and the cropped image is shown.     

Original Image:

Original Image

Cropped Portion from the Original Image:

Portion of the image cropped using Pillow-The Python Image Processing Library


Copyright 2018 © pythontic.com