O'Reilly logo

Computer Vision with Python 3 by Saurabh Kapur

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Cropping the image

Cropping an image in OpenCV is very easy. It can be done by slicing the image array. Slicing an array is just taking the array values within particular index values. Consider the following example:

>>> import cv2>>> img = cv2.imread("image.jpg")>>> img_crop = img[0:200, 150:350]>>> cv2.imwrite("crop_img.jpg", img_crop)>>> cv2.imshow("crop", img_crop)

The following figure is the output of the preceding code:

Figure 4: The original image is on the left and the output is on the right

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required