python找图

发布时间 2023-04-10 14:44:02作者: CrossPython
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('./check/icon.png', 0)

    res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
    threshold = 0.9
    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)


    print('over')