Search code examples
pythonpyautogui

Pyautogui.locateCenterOnScreen can't locate the image even with low confidence


I'm trying to check if a picture is on the screen in a specific region, here's the code first:

    img = pyautogui.screenshot(region=[t1_x - 20, t1_y - 20, 80, 80])
    display_image(img)
    display_image(turret_1_w_plates_image)
    if pyautogui.locateCenterOnScreen(turret_1_w_plates_image, region=[t1_x - 20, t1_y - 20, 80, 80], confidence = 0.1):
        print('true for 1 ')
        return True
    else:
        print('false for 1')
        return False

The img here is this and the turret_1_w_plates_image is this: enter image description here

I even tried to locate with 0.05 confidence and it would always fail to locate it. The turret_1_w_plates_image here is a screenshot I take in program using pyautogui.screenshot() and the image is never saved anywhere on my pc rather it's held only in the variable. Also tried the method locateOnScreen() and it still failed.

I'm on a 2560x1080 monitor, so also tried taking a screenshot of the screen, cropping it to the region I want, and then trying it, and it still didn't work.

Tried making the image smaller too, still didn't work.


Solution

  • well if these sample images are as is, then the scale is incorrect, like asking to locate a red ball size10' with a red ball size1' it will never agree that those are the same object even though they are for technicalities, you need to have to scale size comparison. like holding a quarter right in front of your eye vs looking at it on the ground from 10 feet away. abjust your paint settings or screen capture to not scale even on crop. i use pyautpgui for application automation extensively and that issue is what I ran into early on, it has to be the same scale(which honestly if you are using the same images attached they are clearly not, the 1X out of 10 is anly due to similar color pattern in the vertical and horizontal ( the edge of red and black)