This simple code snippet crops an image and applies some changes on the cropped portion.
Download the file
Crop.png
from Packt website. The size of this image is400
x 400
pixels. You can also use your own image file.Write the following code in a Python source file. Modify the path of the image file to an appropriate path.
import Image img = Image.open("C:\\images\\Crop.png") left = 0 upper = 0 right = 180 lower = 215 bbox = (left, upper, right, lower) img = img.crop(bbox) img.show()
This will crop a region of the image bounded by
bbox
. The specification of the bounding box is identical to what we have seen in the Capturing screenshots section. The output of this example is shown in the following illustration.Original image (left) and its cropped region (right).