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

查看:42
本文介绍了在 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 显示器(可能在重新调整大小之后).

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 = {'left': 160, 'top': 160, 'width': 200, 'height': 200}

with mss() as sct:
    while True:
        screenShot = sct.grab(mon)
        img = Image.frombytes(
            'RGB', 
            (screenShot.width, screenShot.height), 
            screenShot.rgb, 
        )
        cv2.imshow('test', np.array(img))
        if cv2.waitKey(33) & 0xFF in (
            ord('q'), 
            27, 
        ):
            break

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

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