在windows下面,使用python opencv 进行识别,获取到坐标。
依赖安装:
python">
pip install opencv-python
pip install numpy
pip install pyautogui
pip install pywin32
代码:
python">import cv2
import numpy as np
import pyautogui
import os
import sysdef resource_path(relative_path):""" 获取资源的绝对路径 """try:# PyInstaller 创建的临时文件夹,或者当以 --onefile 模式打包时的路径base_path = sys._MEIPASSexcept Exception:# 如果不是打包后的环境,则使用当前工作目录base_path = os.path.abspath(".")rp = os.path.join(base_path, relative_path)return rpdef preprocess_image(image):"""将图像转换为8位无符号整数类型的灰度图像"""gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)return cv2.convertScaleAbs(gray)def find_image_on_screen(template_path, scale_range=(0.5, 2.0), step=0.1):# 获取屏幕截图并预处理screen = pyautogui.screenshot()screen_np = np.array(screen)screen_gray = preprocess_image(screen_np)# 读取模板图像并预处理template = cv2.imread(template_path, cv2.IMREAD_GRAYSCALE)if template is None:raise ValueError("无法加载模板图像")template = cv2.convertScaleAbs(template)found = Nonefor scale in np.arange(scale_range[0], scale_range[1] + step, step):resized = cv2.resize(screen_gray, (0, 0), fx=scale, fy=scale)if resized.shape[0] < template.shape[0] or resized.shape[1] < template.shape[1]:breakres = cv2.matchTemplate(resized, template, cv2.TM_CCOEFF_NORMED)min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)if found is None or max_val > found[0]:found = (max_val, max_loc, scale)if found:_, max_loc, scale = foundtop_left = (int(max_loc[0] / scale), int(max_loc[1] / scale))bottom_right = (int((max_loc[0] + template.shape[1]) / scale), int((max_loc[1] + template.shape[0]) / scale))center = ((top_left[0] + bottom_right[0]) // 2, (top_left[1] + bottom_right[1]) // 2)return centerelse:return None# 使用函数
template_path = resource_path('send_chat2.png')
pyautogui.sleep(2)
center = find_image_on_screen(template_path)
if center:print(f"Found image at: {center}")pyautogui.moveTo(center[0], center[1])
else:print("Image not found.")