一、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("未找到指定圖片")
?