工具–Py即可
重点怎么获取窗口句柄?
使用 spyxx 可以获得句柄 (相当一个窗口的ID,无论窗口怎么变化ID不变我们都可以找到该窗口的详细信息)
替换句柄就可以,也可以不用句柄之间改截图区域
实战图片
import pygetwindow as gw
import pyautogui
import time
import numpy as npdef calculate_image_difference(image1, image2):gray_image1 = image1.convert('L')gray_image2 = image2.convert('L')array1 = np.array(gray_image1)array2 = np.array(gray_image2)hist_diff = np.sum(np.abs(np.histogram(array1, bins=256, range=(0, 256))[0] - np.histogram(array2, bins=256, range=(0, 256))[0]))diff_percentage = (hist_diff / float(array1.size)) * 100return diff_percentage# 使用窗口句柄
window_handle = 0x000715F0while True:# 获取窗口对象window = gw.Window(window_handle)# 获取窗口的位置和大小x, y, width, height = window.left, window.top, window.width, window.height# 设置要截图的区域region = (x, y, width, height)# 获取当前截图current_screenshot = pyautogui.screenshot(region=region)# 等待一段时间,可以根据实际情况调整time.sleep(1)# 再次获取窗口的位置和大小x, y, width, height = window.left, window.top, window.width, window.height# 再次设置要截图的区域region = (x, y, width, height)# 再次获取截图updated_screenshot = pyautogui.screenshot(region=region)# 计算图像差异百分比diff_percentage = calculate_image_difference(current_screenshot, updated_screenshot)# 如果差异超过20%if diff_percentage > 20.0:# 保存当前截图timestamp = time.strftime("%Y%m%d%H%M%S")file_name = f"screenshot_{timestamp}.png"updated_screenshot.save(file_name)print(f"截屏已保存为 {file_name}, 图像差异:{diff_percentage:.2f}%")
如果想一点像素的改变也截图可以更改这里,改为0就可以
# 如果差异超过20%if diff_percentage > 20.0:# 保存当前截图timestamp = time.strftime("%Y%m%d%H%M%S")file_name = f"screenshot_{timestamp}.png"updated_screenshot.save(file_name)print(f"截屏已保存为 {file_name}, 图像差异:{diff_percentage:.2f}%")