Search code examples
pythonimagedetectionpyautogui

Python: How can I find an image on screen by using: pyautogui lib?


The code is:

import pyautogui
startButton = pyautogui.locateOnScreen('start.png')
print startButton

Or:

import pyautogui
startButton = pyautogui.locateCenterOnScreen('start.png')
print startButton

The output is:

None

Note: the correct syntax seems to be in place according to the documentation.

Note: I have tried also with image full path. The image is on the screen and it is not overlapped by other images. The pil library is also installed. Other pyautogui features work (including taking screenshot)

Please let me know what I am missing out. Or please suggest another Python library for image detection.


Solution

  • Here is the syntax I use for this:

    import pyautogui
    start = pyautogui.locateCenterOnScreen('start.png')#If the file is not a png file it will not work
    print(start)
    pyautogui.moveTo(start)#Moves the mouse to the coordinates of the image
    

    If you are using multiple monitors at the same time it only scans the primary one.

    This program scans the pixels of your screen and color matches pixels with your PNG file. If the image color(shadows of the image, the image is changing colors, etc.) changes in any way it will reply with "None".