0

I am trying to get this code below working.

It is grabbing a screen shot and attempting to show just one part of the resulting image.

from PIL import ImageGrab
import numpy as np
import cv2
img = ImageGrab.grab(bbox=(320,190,1500,800)) 
img_np = np.array(img) 
cropped = img_np[10:10, 10:10]
cv2.imshow("cropped", cropped)
cv2.waitKey(0)
cv2.destroyAllWindows()

It returns the error below

Traceback (most recent call last):
  File "/home/nono/Desktop/tmp/belote/t.py", line 8, in <module>
    cv2.imshow("cropped", cropped)
cv2.error: OpenCV(4.5.4) ./modules/core/src/array.cpp:2494: error: (-206:Bad flag (parameter or structure field)) Unrecognized or unsupported array type in function 'cvGetMat'

Would you be able to tell what is wrong here? How can I get my cropped image to show with cv2?

(obviously cropping directly from ImageGrab is not an option as I have plan for multiple crop later).

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Noel
  • 3,749
  • 3
  • 22
  • 21
  • [mre] (debugging) required. what do you think is the result of `cropped = img_np[10:10, 10:10]`? `cropped.shape` is degenerate, it contains 0-sized dimensions. imshow does not show empty pictures. hence the exception. – Christoph Rackwitz Sep 02 '23 at 23:34

0 Answers0