Search code examples
pythonpyautogui

pyautogui.locateAllOnScreen can't locate the image


I'm building a bot a to play the following game called Sushi Go Round. I was using function locateAllOnScreen to locate the orders.

The code is like following:

import pyautogui

onigiri = pyautogui.locateAllOnScreen('onigiri.png')

print(onigiri)

The onigiri.png was cropped out from the third customer. locateAllOnScreen could only locate the third customer's order but not the forth customer's even though their images (orders) were exactly the same.

  1. Why didn't the function locate all images even though the images were the same?

  2. Or were the images actually different?

I'm frustrated. Please help. Thank you very much!

Sushi Go Round

Sushi Go Round

onigiri.png

onigiri.png


Solution

  • For us (humans) images seem identical but for computer they are different if even single pixel is different.

    If I use confidence=0.9 then it finds two items.

    import pyautogui
    
    onigiri = pyautogui.locateAllOnScreen('onigiri.png', confidence=0.9)
    
    for item in onigiri:
        print(item)
    

    Result:

    Box(left=849, top=400, width=55, height=53)
    Box(left=988, top=400, width=55, height=53)
    

    Doc Screenshot:

    The optional "confidence" keyword argument specifies the accuracy with 
    which the function should locate the image on screen. This is helpful 
    in case the function is not able to locate an image due to negligible 
    pixel differences:
    

    For other users:

    Programming a Bot to Play the "Sushi Go Round" Flash Game - The Invent with Python Blog