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 cropReturns : An Image object which represents the cropped image.
Example 1:
# import Image modulefrom PIL import Image # open the imageImage1 = Image.open('D:/cat.jpg') # crop the imagecroppedIm = Image1.crop((130, 120, 200, 200)) # show the imagecroppedIm.show() |
Input Image :
Output :
Example 2:
# import Image modulefrom PIL import Image # open the imageImage1 = Image.open('D:/cat.jpg') # crop the imagecroppedIm = Image1.crop((130, 50, 250, 150)) # show the imagecroppedIm.show() |
Input Image :
Output :
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


