Search code examples
pythonopencvpython-imaging-librarypyautogui

python script to identify when an image is present in an area on the screen


so I want the program to actively look for an image I give it in a certain area on the screen. I want to then have code run if it is on the screen and other code to run if it's not

I have been trying to do this with pyautogui but it does not seem to be working.

pyautogui.locateCenterOnScreen('F.png', confidence = .7, region = (275, 259, 1011, 482))

when I print this and it does not find the image the output is: None when I print this and it does find the image it will output: Point(x=297, y=266)

so I set up an if statement

findImg = pyautogui.locateCenterOnScreen('F.png', confidence = .7, region = (275, 259, 1011, 482))

print (findImg)

if findImg == 'None':
   print ("not found")

This prints None but does not print not found

how can I fix this or is there a better way of doing it?


Solution

  • that is happening because 'None' is in quotes so it is a string, not None. try if findImg == None: instead. although a better way of doing it would probable be moving print(findImg) to an else: block so it dosn't print "None" and "not found"

    if findImg == None:
       print ("not found")
    else:
       print(findImg)