繁体   English   中英

Pyautogui:具有图像识别功能的自动点击器如何制作仅在识别图像时才运行的 function?

[英]Pyautogui : Autoclicker with Image recognition How do I make a function I made only run if an image is recognised?

我是 python 的新手,我知道我的代码真的很乱。 我需要帮助,我希望下面的 function 仅在我设置的图像被识别时运行mc_ok_button.png但这有点令人困惑,因为我希望它加入我已经制作的循环。 如果我将代码放在循环中,它不会运行它下面的函数的 rest 吗?

我的图像识别function:

import pyautogui as pg
import pydirectinput as pd
import time

ax,ay = pg.locateCenterOnScreen('mc_ok_button.png',confidence=.6)

def function_when_restart():
    print(ax,ay)
    pd.click(ax,ay)
    print('Waiting to return back to the server...')
    time.sleep(8)
    pd.moveTo(892,531)
    print('finding server...')
    pd.click()
    print('connecting to the server...')

function_when_restart()

然后我希望function_when_restart() function 仅在mc_ok_button.png图像被识别并在function_when_restart()运行时停止project()fix_tool()时运行,然后继续返回make project()fix_tool()

from time import monotonic
from time import monotonic, sleep

start = monotonic()
        while monotonic() - start < (60) : #Run for exactly 1 minute
            # I want the image recognition to always run and if the image pops up I want 
            project()
            sleep(5)
            fix_tool()

下面可以帮忙吗?

while True:
     image = pyautogui.locateOnScreen('image.png')
     if image != None:
         project()
         sleep(5)
         fix_tool()
     else:
        pass

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM