How to Crop an Image in Python

Spread the love

In this post, you will learn how to crop an image in python.

Crop Image in Python –

An Image is represented as a numpy array, so we can easily crop an image just by slicing the array.

# import libraries
import cv2
import numpy as np
import matplotlib.pyplot as plt

# load image
image = cv2.imread('maldives.jpg', cv2.IMREAD_GRAYSCALE)

output - 
(669, 1000)

Now we can crop the image by slicing the array.

# only keep the first half
image_cropped = image[:,:500]

# show the image
plt.imshow(image_cropped, cmap='gray')

Leave a Reply