当连接到我的显示器上时,PyAutoGui可以完美地找到按钮,但在我的笔记本电脑上却找不到屏幕上的图像。
上下文:我只是在自动运行Clean My Mac。
代码如下:
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)我想知道这是不是因为它们之间的像素有点差,但我也使用了灰度,这应该会对此有所帮助。
发布于 2019-05-27 17:27:07
当从显示器切换到笔记本电脑时,该按钮可能会缩放,因此pyautogui找不到较小的图像。您将需要创建一个检查(最有可能是pyautogui.size()),以查看您是否在使用笔记本电脑并使用较小的图像。
https://stackoverflow.com/questions/56092123
复制相似问题