【发布时间】:2023-01-14 15:44:21
【问题描述】:
我需要能够尽可能快地返回某个像素的 RGB,但是下面的脚本太慢了(每秒返回 RBG 的速度不够快。)
while True:
x = 960
y = 540
rgb = PIL.ImageGrab.grab().load()[x,y]
if rgb == (xxx,xxx,xxx):
mouse.click('left')
time.sleep(0.1)
else:
print(rgb)
有一篇帖子解决了这个问题https://stackoverflow.com/questions/44140586/imagegrab-grab-method-is-too-slow,但是没有一个解决方案清楚地说明如何返回像素/区域的 RGB,只能截图。
我试过使用脚本
with mss.mss() as sct:
# Get a screenshot of the center pixel of the first monitor
sct_img = sct.grab(sct.monitors[1])
monitor = {"top": 540, "left": 960, "width": 1, "height": 1}
sct_img = sct.grab(monitor)
output = "sct-{top}x{left}_{width}x{height}.png".format(**monitor)
但还没有完全弄清楚返回所选像素的 RGB。
如果我使用第二个脚本然后将输出转换为 RGB 可能会起作用,但我不太确定我将如何去做。
【问题讨论】:
-
ImageGrab 抓取整个屏幕。如果你想抓取一个像素,你可能需要使用操作系统特定的 API。你在 Windows 上吗?
-
您需要深入研究 Windows API 才能比屏幕抓取更快地完成它。不确定 Python 是否是最好的语言。
-
如果他们在 Windows 上,使用几乎每个人都使用的 PyWIn32 模块实际上相当容易。它只是 GetDC 和 GetPixel。它仍然不会很快;显卡制造商优化了进入屏幕,而不是来自屏幕。
-
回复@TimRoberts 是的,我在 Windows 上,什么样的 API 和编程语言可以工作,因为我知之甚少,但绝对愿意做一些研究并弄清楚。感谢使用 pywin32 的建议,我肯定会试一试并运行一些测试以查看哪个最快。
标签: python performance python-imaging-library rgb