在Python中从屏幕捕获视频数据 [英] Capture video data from screen in Python

查看:559
本文介绍了在Python中从屏幕捕获视频数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python是否有办法(至少在OpenCV或PIL中)连续抓取全部或部分屏幕的帧,至少以15 fps或更高的速度?我已经看到它是用其他语言完成的,因此从理论上讲应该是可能的.

Is there a way with Python (maybe with OpenCV or PIL) to continuously grab frames of all or a portion of the screen, at least at 15 fps or more? I've seen it done in other languages, so in theory it should be possible.

我不需要将图像数据保存到文件中.实际上,我只希望它输出一个包含原始RGB数据的数组(如numpy数组或类似的东西),因为我将只将其发送到大型LED显示器(可能在调整大小后). /p>

I do not need to save the image data to a file. I actually just want it to output an array containing the raw RGB data (like in a numpy array or something) since I'm going to just take it and send it to a large LED display (probably after re-sizing it).

推荐答案

还有另一种解决方案,其中 mss 可以提供更好的帧速率. (在装有MacOS Sierra的Macbook Pro上进行了测试)

There is an other solution with mss which provide much better frame rate. (Tested on a Macbook Pro with MacOS Sierra)

import numpy as np
import cv2
from mss import mss
from PIL import Image

mon = {'top': 160, 'left': 160, 'width': 200, 'height': 200}

sct = mss()

while 1:
    sct.get_pixels(mon)
    img = Image.frombytes('RGB', (sct.width, sct.height), sct.image)
    cv2.imshow('test', np.array(img))
    if cv2.waitKey(25) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        break

这篇关于在Python中从屏幕捕获视频数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆