TypeError: Expected cv::UMat for argument 'mat'

Why I'm getting this error, when I want to just screen shot by python script?

import cv2
from PIL import ImageGrab 

img = ImageGrab.grab(bbox=(0, 0, 512, 512))
cv2.imshow('image', img)
TypeError: Expected cv::UMat for argument 'mat'
opencvpillowpil
3 votesJW234.00
1 Answers
JO220.00
3

You get the error, because OpenCV needs a numpy array to show as an image, but ImageGrab returns a Pillow object. Just add a convertion script before it.

import cv2
import numpy as np
from PIL import ImageGrab 

img = ImageGrab.grab(bbox=(0, 0, 512, 512))
img2 = np.array(img)
cv2.imshow('image', img2)
Reply
Couldn't find what you were looking for?and we will find an expert to answer.
How helpful was this page?