Search code examples
pythoncomputer-visionpyautoguimit-scratch

PyAutoGui LocateOnScreen() returning "None"


I have been working on a Python script that clicks on boxes on the screen. The box clicking game is one that I have developed using https://scratch.mit.edu. Here is a link to the game: https://scratch.mit.edu/projects/295212078/fullscreen/. My code is not working though. Here is my code:

import pyautogui
import time

time.sleep(3)

pyautogui.moveTo(310, 120, duration=0.25)
pyautogui.click()

while True:
    loc = pyautogui.locateOnScreen("Click box.jpg")
    print(loc)
    if (loc != None):
        center = pyautogui.center(loc)
        print(center)
        pyautogui.moveTo(center, duration=0.25)
        pyautogui.click()

The problem with this code is that the code, instead of getting the coordinates for the click boxes, it gets "None". Here is the image I am sensing for: Click box.jpg

The thing that is making me wonder if this is scratch's issue is the fact that when I opened the image in my browser, my mouse moved toward it and clicked on it.

Another guess I have is that the image is larger when I opened it in my browser rather than in the game. I actually believe that is the reason, but I am not sure how to get around that!

(My current version of Python is 3.7.2)

I appreciate any help you can give!


Solution

  • I realized all that I had to do was shrink down the image! Should have thought about that before!