# -*- coding:utf-8 -*-
import time
import random
import aircvas ac
import pyautogui
def matchImg(imgsrc, imgobj, confidence=0.9):# imgsrc=原始图像,imgobj=待查找的图片,confidence=设置匹配系数
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)
网友评论