要找的图为计算机,也就是icon.png,对应的图标为:
需要安装的依赖:
- pip install cv2
- pip install PIL
- pip install pyautogui
代码如下:
- import cv2
- from PIL import ImageGrab
- import numpy as np
- import pyautogui
-
- if __name__ == '__main__':
-
- im = ImageGrab.grab()
- im.save('./res/screen.png', 'png')
-
- img_rgb = cv2.imread('./res/screen.png')
-
- # 所有操作在灰度版中进行
- img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
- template = cv2.imread('./res/icon.png', 0)
-
- res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
- threshold = 0.7
- loc = np.where(res >= threshold)
-
- for pt in zip(*loc[::-1]):
- print(pt[0], pt[1])
- # pyautogui.moveTo(pt[0] + template.shape[0] / 2, pt[1] + template.shape[1] / 2)
- pyautogui.doubleClick(pt[0] + template.shape[0] / 2, pt[1] + template.shape[1] / 2)
-
- pass
-
-
- print('over')
- pass