Search code examples
pythonpyautogui

how to make the program find an image with pyautogui?


In the code below as you can see i'm trying to find an image in the screen with a .png(that i didn't add because i don't think it's the problem) but it doesn't find anything

import pyautogui as pt
from time import sleep

class Clicker:
    def __init__(self, target_png, speed):
        self.target_png = target_png
        self.speed = speed
        pt.FAILSAFE = True

    def nav_to_image(self):
        try:
            position = pt.locateOnScreen(self.target_png, confidence=.8)
            pt.moveTo(position[0] + 15, position[1] + 15, duration=self.speed)
            pt.doubleClick()

        except:
            print('No img found')
            return 0

if __name__=='__main__':
    sleep(1)
    clicker = Clicker('accetta.png', speed=.001)

    end = 0
    while True:
        sleep(1)
        if clicker.nav_to_image() == 00:
            end += 1

        if end > 5:
            break

Solution

  • I see that you confidence is set to 80%. It's a little tricky when you're not looking for the exact match. Have you tested the code with an image that doesn't require confidence to work?

    Sometimes there is a difference in the hue. Try locateOnScreen(image, grayscale=False) and see if that makes a difference.

    It would help if you included both the image you're trying to find and a copy of the screenshot in order to reproduce the error.