'PyAutoGui can't locate image in fullscreen program

I'm trying to automate a task in a full screen game as a QOL improvement. This means reading a value from a screenshot of the game, and if my set conditions are met, locate the image and right click on it.

I'm having trouble using the pyautogui locateCenterOnScreen function, it always returns None.

This is the part I'm having trouble with :

import pyautogui
import time

pyautogui.size()
(1920, 1080)
pyautogui.FAILSAFE = True

time.sleep(3)

imageLocation = pyautogui.locateCenterOnScreen('myfilepath')

print (imageLocation)

The delay is there to make sure I have enough time to open the game. I've used pyautogui to take screenshots in-game in order to use OCR to read from it, which seems to be working just fine. But for some reason I can't make the image recognition work.

I've tried to just use a small portion of the image that always remains static, and even used pyautogui to screenshot the region to see if using it's own generated image of the object would work. I've tried having the game in windowed mode, but nothing seems to work. Not sure if it matters, but I've tried both JPG and PNG file format for the images.

Can anyone see where I'm going wrong?

What I'm trying to locate

What I'm using to locate it

It works when I have PyCharm open and search for this :

PyCharm

So the function seems to be working as it should, but it can't find it in game. Also, I've tried using the grey scale option but it still didn't work.

I'd really appreciate some help.

Thanks in advance

Ziggy



Solution 1:[1]

Try pyautogui's confidence parameter.

pyautogui.locateCenterOnScreen('myfilepath',confidence=0.9)

confidence=0.9 means %90 of pixels are same, return true else return false. you can change confidence value whatever you want.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hasan Onur ATAÇ