D
D
Delorith2019-11-17 16:44:12
Python
Delorith, 2019-11-17 16:44:12

How to make a loop that runs until a certain point?

There is the following function to search for an image in an image:

def find_img(template_path,sleep):
    img_rgb = pyautogui.screenshot() # Скриншот текущего экрана
    img_rgb.save('D:/images/general.jpg')  
    img_rgb = cv2.imread('D:/images/general.jpg') # Шаблон

    template = cv2.imread(template_path)  # Изображение, искомое в шаблоне

    w, h = template.shape[:-1]


    res = cv2.matchTemplate(img_rgb, template, cv2.TM_CCOEFF_NORMED)

    if str(res[0]) != '[1. 1. 1. ... 1. 1. 1.]':
        threshold = .7

        loc = np.where(res >= threshold)  
        # Проверка совпадения
        for pt in zip(*loc[::-1]):
            x = int(pt[0])
            y = int(pt[1])
            print(x, y) # Координаты совпадения
            pyautogui.moveTo(x, y, duration=0.7)  # Переход по координатам
            pyautogui.click()
            time.sleep(sleep) #Ожидание sleep секунд.
            break
        else:
            print('Image not found')

What she does?
Takes a screenshot of the screen and saves it to the general.jpg file, then looks for another image in it and, if successful, moves the mouse cursor and clicks; if it fails, it displays Image not found. Roughly speaking, it looks for a button on the screen and presses it.
I need to make it try to find a button on the screen until it finds it. More precisely, while there is no defeat button, find other buttons and press them, and when the defeat button appears, the cycle ends its work.
Perhaps the answer is very easy and I do not see something obvious, but I ask for help in solving this problem.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question