logo
summer

太阳当空照,花儿对我笑。

Python找图

1/6/2023, 4:16:26 PM
  1. 首页
  2. /
  3. 正文
import cv2
import pyautogui
from PIL import ImageGrab
import time
import numpy

img = ImageGrab.grab()
# img.save("screen.png")

# 图片和路径请勿使用中文
img_wait = cv2.imread("wait.png", cv2.IMREAD_GRAYSCALE)
img_checked = cv2.imread("checked.png", cv2.IMREAD_GRAYSCALE)
img_play = cv2.imread("play.png", cv2.IMREAD_GRAYSCALE)
tmp = img_wait
i = 2

tasks = [img_wait, img_play, img_checked]
while True:
    time.sleep(1)
    img = ImageGrab.grab()
    img_screen = cv2.cvtColor(numpy.asarray(img), cv2.COLOR_RGB2BGR)
    img_screen_gray = cv2.cvtColor(img_screen, cv2.COLOR_BGR2GRAY)

    res = cv2.matchTemplate(img_screen_gray, tasks[i], cv2.TM_CCOEFF_NORMED)

    _, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    if max_val > 0.95:
        # 找到
        i = i + 1
        i = i % 3
        print(max_loc)
        pyautogui.moveTo(max_loc)
        pyautogui.click()

热门文章
标签云
© 2021 Copyright 本站由 upyun 提供储存服务