# -*- coding:utf-8 -*-
import time
import random
import aircvas ac
import pyautogui
def matchImg(imgsrc, imgobj, confidence=0.9):# imgsrc=原始圖像寸爆,imgobj=待查找的圖片,confidence=設(shè)置匹配系數(shù)
? ? imsrc = ac.imread(imgsrc)
imobj = ac.imread(imgobj)
match_result = ac.find_template(imsrc, imobj, confidence)
if match_resultis not None:
match_result['shape'] = (imsrc.shape[1], imsrc.shape[0])# 0為高步做,1為寬
? ? ? ? left = match_result['rectangle'][0][1]
right = match_result['rectangle'][3][1]
top = match_result['rectangle'][0][0]
buttom = match_result['rectangle'][3][0]
pyautogui.moveTo(top, left)
time.sleep(2)
pyautogui.moveTo(top, right)
time.sleep(2)
pyautogui.moveTo(buttom, left)
time.sleep(2)
pyautogui.moveTo(buttom, right)
time.sleep(2)
pyautogui.moveTo(random.randint(top,buttom), random.randint(left,right))
return match_result
if __name__ =='__main__':
p = matchImg(r"D:\code\testpic\desktop.png",
? ? ? ? ? ? ? ? r"D:\code\testpic\code.png")
print(p)