Skip to main content

How to crop image in OpenCV Python?

Crop image in OpenCV

Cropping is the process of selecting and extracting a specific region from the image. For example, we may want to crop a car in an image or we can crop a house in the image.

Read a image:

img = cv2.imread("flower.jpg")
cv2.imshow("image", img)

Select the boundaries of the image to be cropped:

x=50
y=50
h=120
w=120

Cropping:

crop_img = img[y:y+h, x:x+w]
cv2.imshow("cropped", crop_img)

Result:


All Code:

import cv2
img = cv2.imread("flower.jpg")
cv2.imshow("image", img)
x=50
y=50
h=120
w=120
crop_img = img[y:y+h, x:x+w]
cv2.imshow("cropped", crop_img)
cv2.waitKey(0)







Comments