1

I am using the following code to crop an image . when I print the cropped,Its showing and empty array. Could anyone help me to get this cropping done right

import cv2
import pytesseract


image = cv2.imread(r'C:\Users\Ramesh\Desktop\Parsing_Project\Resumes_jpg\Akhil\output1.jpg')
image = cv2.resize(image,(800,740))

cropped = image[292:37, 564:65]

cv2.imshow("cropped",cropped)

cv2.waitKey(0)

This is the error screenshot

dimay
  • 2,768
  • 1
  • 13
  • 22

1 Answers1

4

When you read an image with OpenCV Python you get a Numpy array. Cropping from this array (it's called slicing with Numpy) does not work that way. You do not define start and length, but start and end point:

cropped = image[start_row:end_row, start_col:end_col]

Be aware that the end row/col itself are exclusive so not included. What you get with your example is of course an empty array which is impossible to display with OpenCV.

gfkri
  • 2,151
  • 21
  • 23
  • What I did was trying to crop using the coordinates. Is there anyway to pass coordinates and crop ? – Rahul Ramesh Jan 13 '21 at 07:22
  • 1
    By coordinates you mean x/y and width/height? It is as easy as it can get: `image[y:y+height, x:x+width]`. – gfkri Jan 13 '21 at 07:35