Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Crop image using pillow

Python | Crop image using pillow

In this article, we will learn to crop an image using pillow library. Cropping an image means to select a rectangular region inside an image and removing everything outside the rectangle. To crop an image we make use of crop() method on image objects.

Syntax : IMG.crop(box_tuple)

Parameters :
Image_path- Location of the image
IMG- Image to crop
box_tuple- [left, up, right, bottom] of the image to crop

Returns : An Image object which represents the cropped image.

Example 1:




# import Image module
from PIL import Image
  
# open the image
Image1 = Image.open('D:/cat.jpg')
  
# crop the image
croppedIm = Image1.crop((130, 120, 200, 200))
  
# show the image
croppedIm.show()


Input Image :

Output :

Example 2:




# import Image module
from PIL import Image
  
# open the image
Image1 = Image.open('D:/cat.jpg')
  
# crop the image
croppedIm = Image1.crop((130, 50, 250, 150))
  
# show the image
croppedIm.show()


Input Image :

Output :

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments