使用 OpenCV 和 Python-2.7 进行屏幕截图 [英] Screen Capture with OpenCV and Python-2.7
问题描述
我使用的是 Python 2.7 和 OpenCV 2.4.9.
我需要捕获显示给用户的当前帧,并将其加载为 Python 中的 cv::Mat 对象.
I need to capture the current frame that is being shown to the user and load it as an cv::Mat object in Python.
你们知道递归的快速方法吗?
Do you guys know a fast way to do it recursively?
我需要类似于下面示例中所做的事情,它从网络摄像头中递归地捕获 Mat 帧:
I need something like what's done in the example below, that captures Mat frames from a webcam recursively:
import cv2
cap = cv2.VideoCapture(0)
while(cap.isOpened()):
ret, frame = cap.read()
cv2.imshow('WindowName', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
cap.release()
cv2.destroyAllWindows()
break
在示例中,它使用 VideoCapture 类来处理从网络摄像头捕获的图像.
In the example it's used the VideoCapture class to work with the captured image from the webcam.
使用 VideoCapture.read() 总是会读取新帧并将其存储到 Mat 对象中.
With VideoCapture.read() a new frame is always being readed and stored into a Mat object.
我可以将 printscreens 流" 加载到 VideoCapture 对象中吗?我可以在 Python 中使用 OpenCV 创建计算机屏幕的流媒体,而不必每秒保存和删除大量 .bmp 文件吗?
Could I load a "printscreens stream" into a VideoCapture object? Could I create a streaming of my computer's screen with OpenCV in Python, without having to save and delete lots of .bmp files per second?
我需要这些帧是 Mat 对象或 NumPy 数组,以便我可以执行一些计算机视觉实时处理此帧的例程.
I need this frames to be Mat objects or NumPy arrays, so I can perform some Computer Vision routines with this frames in real time.
推荐答案
这是我使用 @Raoul 提示编写的解决方案代码.
That's a solution code I've written using @Raoul tips.
我使用 PIL ImageGrab 模块来抓取打印屏幕帧.
I used PIL ImageGrab module to grab the printscreen frames.
import numpy as np
from PIL import ImageGrab
import cv2
while(True):
printscreen_pil = ImageGrab.grab()
printscreen_numpy = np.array(printscreen_pil.getdata(),dtype='uint8')
.reshape((printscreen_pil.size[1],printscreen_pil.size[0],3))
cv2.imshow('window',printscreen_numpy)
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
这篇关于使用 OpenCV 和 Python-2.7 进行屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!