Reputation: 97
PyAutoGui finds the button perfectly when plugged into my monitor, which I code on, but fails to find the image on screen while on my laptop.
Context: I'm simply automating running through Clean My Mac automatically.
Here's the code:
import pyautogui
import subprocess
import time
import cv2
from termcolor import colored
print('Starting'.format(), end='\r')
subprocess.call(
["/usr/bin/open", "/Applications/CleanMyMac.app"]
)
time.sleep(3)
print('Starting'.format(), end='\r')
scanButton = None
scanButton = pyautogui.locateOnScreen('scan.png', grayscale = True,
confidence = .9)
scan = None
scan = pyautogui.center(scanButton)
pyautogui.moveTo(scan)
pyautogui.click(scan)
runButton = None
while runButton == None:
try:
runButton = pyautogui.locateOnScreen('run.png', grayscale = True, confidence = .9)
clean = None
clean = pyautogui.center(runButton)
print(('Scan'), ('['), colored('Complete','green'), (']'))
except TypeError:
runButton = None
print('Scanning...'.format(), end='\r')
pyautogui.moveTo(clean)
pyautogui.click(clean)
ignoreButton = None
time.sleep(3)
while ignoreButton == None:
try:
print('Ignoring Chrome [running]'.format(), end='\r')
ignoreButton = pyautogui.locateOnScreen('ignore.png', grayscale = True, confidence = .9)
ignore = None
ignore = pyautogui.center(ignoreButton)
print(('Ignoring Chrome'), ('['), colored('Complete','green'), (']'))
pyautogui.moveTo(ignore)
pyautogui.click(ignore)
except TypeError:
ignoreButton = True
print(('Ignoring Chrome'), ('['), colored('Not Required','green'), (']'))
completeButton = None
while completeButton == None:
try:
completeButton = pyautogui.locateOnScreen('complete.png', grayscale = True, confidence = .9)
complete = None
complete = pyautogui.center(completeButton)
except TypeError:
completeButton = None
print('Optimizing System...'.format(), end='\r')
print(('System Status'), ('['), colored('Optimized','green'), (']'))
print('All Systems Go, Captain!')
closeButton = pyautogui.locateOnScreen('close.png')#, grayscale = True, confidence = .8)
close = pyautogui.center(closeButton)
pyautogui.moveTo(close)
pyautogui.click(close)
I'm wondering if it's because the pixels are a bit off between them but I'm also using grayscale, which should help with that.
Upvotes: 0
Views: 321
Reputation: 11
The button is probably scaling when changing from monitor to laptop, so pyautogui can't find the smaller image. you will need to create a check (most likely pyautogui.size()) to see if you're on your laptop and use a smaller image.
Upvotes: 1