PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. PIL.Image.crop() method is used to crop a rectangular portion of any image.
Syntax: PIL.Image.crop(box = None)
Parameters:
box – a 4-tuple defining the left, upper, right, and lower pixel coordinate.
Return type: Image (Returns a rectangular region as (left, upper, right, lower)-tuple).
Return: An Image object.
Code #1:
Python3
# Importing Image class from PIL modulefrom PIL import Image# Opens a image in RGB modeim = Image.open(r"C:\Users\Admin\Pictures\geeks.png")# Size of the image in pixels (size of original image)# (This is not mandatory)width, height = im.size# Setting the points for cropped imageleft = 5top = height / 4right = 164bottom = 3 * height / 4# Cropped image of above dimension# (It will not change original image)im1 = im.crop((left, top, right, bottom))# Shows the image in image viewerim1.show() |
Original Image

Output:

Code #2:
Python3
# Importing Image class from PIL modulefrom PIL import Image# Opens a image in RGB modeim = Image.open(r"C:\Users\Admin\Pictures\network.png")# Setting the points for cropped imageleft = 155top = 65right = 360bottom = 270# Cropped image of above dimension# (It will not change original image)im1 = im.crop((left, top, right, bottom))# Shows the image in image viewerim1.show() |
Original 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


