在循环中加速 pyautogui 屏幕截图
Speeding up a pyautogui screenshot in a loop
所以,我这里有这个程序,我将用它来制作贪吃蛇游戏的机器人。但它实在是太慢了。关于如何加快它的任何想法?
import pyautogui
running = True
color = (231, 71, 29)
while running == True:
s = pyautogui.screenshot()
for x in range(s.width):
for y in range(s.height):
if s.getpixel((x, y)) == color:
pyautogui.click(x, y)
s = pyautogui.screenshot()
print(x, y)
您可以限制截图区域。根据pyautogui's docs:
On a 1920 x 1080 screen, the screenshot()
function takes roughly 100 milliseconds - it’s not fast but it’s not slow.
There is also an optional region
keyword argument, if you do not want a screenshot of the entire screen. You can pass a four-integer tuple of the left, top, width, and height of the region to capture:
>> import pyautogui
>> im = pyautogui.screenshot(region=(0,0, 300, 400))
如果能只定义贪吃蛇游戏的截图区域,速度会更快。
所以,我这里有这个程序,我将用它来制作贪吃蛇游戏的机器人。但它实在是太慢了。关于如何加快它的任何想法?
import pyautogui
running = True
color = (231, 71, 29)
while running == True:
s = pyautogui.screenshot()
for x in range(s.width):
for y in range(s.height):
if s.getpixel((x, y)) == color:
pyautogui.click(x, y)
s = pyautogui.screenshot()
print(x, y)
您可以限制截图区域。根据pyautogui's docs:
On a 1920 x 1080 screen, the
screenshot()
function takes roughly 100 milliseconds - it’s not fast but it’s not slow.There is also an optional
region
keyword argument, if you do not want a screenshot of the entire screen. You can pass a four-integer tuple of the left, top, width, and height of the region to capture:>> import pyautogui >> im = pyautogui.screenshot(region=(0,0, 300, 400))
如果能只定义贪吃蛇游戏的截图区域,速度会更快。