PyAutoGui会找到按钮,但只有在插入显示器后才能找到

时间:2019-05-11 16:14:33

标签: python cv2 pyautogui

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)

我想知道是否是因为它们之间的像素有些偏离,但是我也在使用灰度,这应该可以解决问题。

1 个答案:

答案 0 :(得分:1)

从显示器更改为笔记本电脑时,该按钮可能会缩放,因此pyautogui找不到较小的图像。您将需要创建一个检查(很可能是pyautogui.size()),以查看您是否在笔记本电脑上并使用较小的图像。