gpt4 book ai didi

python - ImageGrab.grab() 方法太慢

转载 作者:太空宇宙 更新时间:2023-11-03 14:05:22 25 4
gpt4 key购买 nike

所以我需要每秒获取一堆屏幕截图,例如 5 个。我正在使用它来为游戏编写机器人程序。然而 imagegrab 方法大约需要 0.3 秒,这对我来说太慢了。即使在指定了 bbox 值之后,它仍然需要 0.3 秒。我认为应该提到我在 Mac 上。有没有更好的方法适合我

我什至尝试了 os.system("screencapture filename.png"),它的运行时间为 0.15-0.2 秒,这很好,但我想运行得更快。

最佳答案

另一个解决方案是使用 Python MSS .

from mss import mss
from PIL import Image

def capture_screenshot():
# Capture entire screen
with mss() as sct:
monitor = sct.monitors[1]
sct_img = sct.grab(monitor)
# Convert to PIL/Pillow Image
return Image.frombytes('RGB', sct_img.size, sct_img.bgra, 'raw', 'BGRX')

img = capture_screenshot()
img.show()

此函数可以在我的慢速笔记本电脑上以高达 27 fps 的速度返回屏幕截图。

关于python - ImageGrab.grab() 方法太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44140586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com