.crop()
Anonymous contributor
Published Jun 7, 2024
Contribute to Docs
In the Pillow library, the .crop()
method returns a given image cropped to a specified rectangular area.
Syntax
Image.crop(box=None)
box
: It is a 4-tuple representing the rectangular area of the image to be cropped.
This tuple comprises four integer values, i.e., (left, upper, right, lower)
. They signify the coordinates of the left, upper, right, and lower edges of the area to be cropped, respectively.
Example
The following example uses .crop()
to return an area within the provided image:
from PIL import Image# The original image has a size of 1024x768 pixelswith Image.open('Photo.jpeg') as photo:# Displaying the original imagephoto.show()# Assigning the desired crop region, which can be envisioned as (x1, y1, x2, y2)(left, upper, right, lower) = (100, 100, 600, 600)# Saving the cropped image to the 'croppedVarName' variablecroppedVarName = photo.crop((left, upper, right, lower))# Displaying the cropped imagecroppedVarName.show()
The above code displays two images as the output. Here is the first one, which is the original image:
Here is the second one, which is the cropped image:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.