简体   繁体   English

使用 pyautogui 和 opencv 截屏

[英]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.我尝试使用 opencv 中的不同函数,似乎它们都返回相同的函数。 What do I need to do in order to take a screenshot then work with it in Open CV?我需要做什么才能截取屏幕截图然后在 Open CV 中使用它?

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.经过一番挖掘,我意识到 pyautogui 函数正在使用 Pillow,它提供了一种必须适用于 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM