Crop an image with Python

Python is a powerful programming language that can be used to crop images. In this tutorial, we will show you how to crop an image using the Python programming language and the PIL library:


from PIL import Image
import matplotlib.pyplot as plt


image_path = "cat.jpg" #path to the image to crop

image = Image.open( image_path)


Plot the image:


plt.imshow(image)

And then pass the coordenates to crop:


coords = [1000, 500, 2800 , 2800]# seccion of he image to crop in the form of coordenates 

cropped_image = image.crop(coords)

output_image_path = "/content/cropped_cat.jpg" #path where to save the new image
cropped_image.save(output_image_path)


And the cropped image:

Leave a Reply

Your email address will not be published. Required fields are marked *