Crop an Image Tutorial and Example in Python OpenCV

It is easy to crop a part of image from a big image. In this tutorial, we will use an example to show you how to do.

Crop an Image Tutorials and Examples in Python OpenCV

1.Import opencv

import cv2

2.Read an image and get its width and height

img = cv2.imread("pyimg.jpg")
height, width = img.shape[0:2]

3.Determine the position of cropped image

startRow = int(height*.15)
startCol = int(width*.15)
endRow = int(height*.85)
endCol = int(width*.85)

In this tutorial, we will use the height of width of original image to get the position of cropped image.

4.Get cropped image

croppedImage = img[startRow:endRow, startCol:endCol]

5.Display the original and cropped image

Crop an Image Tutorial and Example in Python OpenCV