5

I am using the following code:

import cv2

import numpy as np

import pyautogui
import sys


img = pyautogui.screenshot()
cv2.imshow('image',img)

When I run this, it tells me

mat is not a numpy array, neither a scalar

I have tried to use different functions from opencv and it seems they all return the same. What do I need to do in order to take a screenshot then work with it in Open CV?

1 Answer 1

8

After some digging, I realise that the pyautogui function is using Pillow which is giving a format that must be adapted to work with opencv.

I added the following code so that it worked:

open_cv_image = np.array(img) 
# Convert RGB to BGR 
open_cv_image = open_cv_image[:, :, ::-1].copy()
1
  • 1
    Something else you could do in order to achieve the same output but more expressive: imageObj = pyautogui.screenshot() cv_imageObj = cv2.cvtColor(numpy.array(imageObj), cv2.COLOR_RGB2BGR)
    – Mat
    Dec 24, 2021 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.