使用 Python 是否可以每秒超过 10 个屏幕截图?

Is faster than 10 screenshots per second possible with Python?

现在我需要一个非常快速的屏幕截图程序来将屏幕截图输入 CNN,该 CNN 将根据屏幕截图更新鼠标移动。我希望对 this paper, and similarly do the steps featured in Figure 6 (without the polar conversion). As a result of needing really fast input, I've searched around a bit and have been able to get this script 中出现的相同类型的行为进行建模,稍作修改即可输出 10fps

from PIL import ImageGrab
from datetime import datetime

while True:
    im = ImageGrab.grab([320, 180, 1600, 900])
    dt = datetime.now()
    fname = "pic_{}.{}.png".format(dt.strftime("%H%M_%S"), dt.microsecond // 100000)
    im.save(fname, 'png') 

我能期待更快吗?如果可以的话,我也愿意使用其他程序。

写入磁盘非常慢,这可能是导致循环花费这么长时间的主要原因。尝试注释掉 im.save() 行并查看可以捕获多少个屏幕截图(添加一个 counter 变量或类似的东西来计算捕获了多少个屏幕截图)。

假设磁盘 I/O 是瓶颈,您需要将这两个任务分开。有一个循环只捕获屏幕截图并将它们存储在内存中(例如以时间戳为键的字典),然后在一个单独的线程中从字典中提取元素并将它们写入磁盘。

如果您以前没有做过很多,请参阅 this question 中关于线程的指示 Python。