Will
Will

Reputation: 41

PyAutoGui always returns none for locateOnScreen

My pyautogui always returns none for images. The images are in the same folder as the program. The image names are the same I have them saved as. The images are up to date and definitely on my screen. Please help, pyautogui always returns none for locateOnScreen. Here is my code:

import time
import sys
import pyautogui

pyautogui.FAILSAFE = True
pyautogui.PAUSE = 1
pyautogui.size()

width, height = pyautogui.size()


y = pyautogui.locateOnScreen('LOLicon.png')
print(y)
for i in range(2):
    x = pyautogui.moveTo(y)
    pyautogui.click(x)
    time.sleep(2)
    pyautogui.doubleClick()

del x
del y

Upvotes: 1

Views: 2671

Answers (1)

user7711283
user7711283

Reputation:

import time
import sys
import pyautogui

pyautogui.FAILSAFE = True
pyautogui.PAUSE = 1
pyautogui.size()

width, height = pyautogui.size()

y = pyautogui.locateOnScreen('LOLicon.png')
print(y)
for i in range(2):
    x = pyautogui.moveTo(y[0:2])
    pyautogui.click(x)
    time.sleep(2)
    pyautogui.doubleClick()

This above works perfectly. The only change in code is x = pyautogui.moveTo(y[0:2]).

So make sure that the image is actually on screen while you are running your script (not hidden by the code editor or other window) and the image content of LOLicon.png is actually what you assume it is.

It could be also helpful to check out if this code:

import pyautogui
im = pyautogui.screenshot(region=(20, 20, 50, 50)) 
im.save("myScreenshot.png")
y = pyautogui.locateOnScreen("myScreenshot.png")
print(y)
x = pyautogui.moveTo(y[0:2])

runs fine without an error. If it does, you can rename myScreenshot.png to LOLicon.png and adjust region=(20, 20, 50, 50) so, that it captures the LOLicon on the screen.

Check out https://pyautogui.readthedocs.io/en/latest/screenshot.html?highlight=save%20image for details of the screenshot functions in pyautogui and make sure the required modules are installed ( Pillow and eventually scrot if you are on Linux ).

Upvotes: 1

Related Questions