python实时捕捉屏幕_在Python中从屏幕捕获视频数据

该博客介绍了如何使用Python的mss库和OpenCV来实现实时抓取屏幕的一部分,至少以15fps的帧率。代码示例展示了如何不保存图像数据到文件,而是直接将RGB数据转换为NumPy数组,以便进一步处理,如显示在大型LED显示屏上。
摘要由CSDN通过智能技术生成

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.

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).

解决方案

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

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值