Only press if 2 images is present

Viewed 90

I am using PYautoGUI and im trying to make a script that will solve a puzzle. The puzzle tells me to tap the pair (what ever image there is two of) I have this code so far:

    if pyautogui.locateCenterOnScreen('TapPair.png', confidence=0.9, region=(689, 250, 640, 1000)) != None:
    for image in images:
        found = list(pyautogui.locateAllOnScreen(image, confidence=0.99, region=(689, 250, 640, 1000)))
        if len(found) >= 2:
            for i in found:
                pyautogui.click(i)
                print("Got a match")
                time.sleep(2)
                break

Problem is: it TAPS every image that it matches with... why?

Edit: here is an example of the puzzleenter image description here

1 Answers

I'm supposing the images from your code is a list of images that you already saved. So I downloaded your image, and save the sun to be the one to be clicked.

From your code I removed the region just to test, but you can use it for a better match. Also I changed locateCenterOnScreen() to locateOnScreen().

My puzzle image:

Puzzle

My sun image:

Sun

My code:

import pyautogui
import time

game = "game.png"
sun = "sun.png"
images = [sun]

print(images)

if pyautogui.locateOnScreen(game, confidence=0.8) != None:
    for image in images:
        found = list(pyautogui.locateAllOnScreen(image, confidence=0.99))
        if len(found) >= 2:
            for position in found:
                pyautogui.click(position)
                print("Got a match")
                time.sleep(2)

I also was getting multiple locations differently from each other and more than two after I decrease confidence, so try to keep the highest confidence you can on found = list(pyautogui.locateAllOnScreen(image, confidence=0.99)).

Also remember to use the images from your screen, because the resolution and pixels will change!

I splited my code and my image into two windows side by side in my screen. When I print(found), I was getting:

[Box(left=1216, top=317, width=130, height=113), Box(left=1529, top=630, width=130, height=113)]

After that, it was clicked on both images correctly!

Related