关键词搜索

源码搜索 ×
×

Python笔记-利用OpenCV的matchTemplate屏幕找图并使用pyautogui点击

发布2021-09-14浏览1846次

详情内容

要找的图为计算机,也就是icon.png,对应的图标为:

 需要安装的依赖:

  1. pip install cv2
  2. pip install PIL
  3. pip install pyautogui

代码如下:

  1. import cv2
  2. from PIL import ImageGrab
  3. import numpy as np
  4. import pyautogui
  5. if __name__ == '__main__':
  6. im = ImageGrab.grab()
  7. im.save('./res/screen.png', 'png')
  8. img_rgb = cv2.imread('./res/screen.png')
  9. # 所有操作在灰度版中进行
  10. img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
  11. template = cv2.imread('./res/icon.png', 0)
  12. res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
  13. threshold = 0.7
  14. loc = np.where(res >= threshold)
  15. for pt in zip(*loc[::-1]):
  16. print(pt[0], pt[1])
  17. # pyautogui.moveTo(pt[0] + template.shape[0] / 2, pt[1] + template.shape[1] / 2)
  18. pyautogui.doubleClick(pt[0] + template.shape[0] / 2, pt[1] + template.shape[1] / 2)
  19. pass
  20. print('over')
  21. pass

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载