一、pyautogui原始逻辑
import pyautogui
# 获取指定图片在屏幕上的位置
image_path = 'path/to/image.png'
target_position = pyautogui.locateCenterOnScreen(image_path)
if target_position is not None:
# 获取偏移量
offset_x = 10
offset_y = 10
# 计算实际点击位置
click_x = target_position.x + offset_x
click_y = target_position.y + offset_y
# 点击指定位置
pyautogui.click(click_x, click_y)
else:
print("未找到指定图片")
二、CV2测试
需要先安装
pip install opencv-python
import pyautogui
import cv2
import numpy as np
# 读取目标图片
target_image = cv2.imread('path/to/image.png')
# 获取屏幕截图
screenshot = pyautogui.screenshot()
screenshot = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2BGR)
# 在屏幕截图中查找目标图片
result = cv2.matchTemplate(screenshot, target_image, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
top_left = max_loc
if max_val > 0.9:
# 获取偏移量
offset_x = 10
offset_y = 10
# 计算实际点击位置
click_x = top_left[0] + offset_x
click_y = top_left[1] + offset_y
# 点击指定位置
pyautogui.click(click_x, click_y)
else:
print("未找到指定图片")
三、pillow和pygetwindows
pip install Pillow
pip install pygetwindow
import pyautogui
from PIL import ImageGrab
import pygetwindow as gw
# 获取指定窗口
target_window = gw.getWindowsWithTitle('Window Title')[0]
# 获取窗口位置和大小
window_rect = target_window.rect
# 获取窗口截图
screenshot = ImageGrab.grab(bbox=(window_rect.left, window_rect.top, window_rect.right, window_rect.bottom))
# 读取目标图片
target_image = Image.open('path/to/image.png')
# 在窗口截图中查找目标图片
result = pyautogui.locate(target_image, screenshot, grayscale=True, confidence=0.9)
if result is not None:
# 获取偏移量
offset_x = 10
offset_y = 10
# 计算实际点击位置
click_x = window_rect.left + result.left + offset_x
click_y = window_rect.top + result.top + offset_y
# 点击指定位置
pyautogui.click(click_x, click_y)
else:
print("未找到指定图片")