Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using pyautogui and opencv for screenshot

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?

like image 724
Richard Avatar asked Mar 13 '23 23:03

Richard


1 Answers

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()
like image 190
Richard Avatar answered Mar 15 '23 11:03

Richard