""" 用于查看截图文件并确认坐标位置的辅助脚本 使用方法: 1. 运行程序后,会打开当前截图 2. 在图片上点击,会显示点击位置的坐标 3. 记录你需要的区域坐标 """ import os from PIL import Image import sys def show_image_coordinates(image_path): """显示图片并允许用户查看坐标""" if not os.path.exists(image_path): print(f"图片文件不存在: {image_path}") return try: img = Image.open(image_path) width, height = img.size print(f"\n图片信息:") print(f" 文件路径: {image_path}") print(f" 尺寸: {width} x {height} 像素") print(f"\n坐标说明:") print(f" - 坐标系原点 (0, 0) 在左上角") print(f" - X 轴向右递增") print(f" - Y 轴向下递增") print(f" - 格式: (x, y) 或 (x1, y1, x2, y2)") print(f"\n如何获取坐标:") print(f" 方法1: 使用Windows画图工具") print(f" 1. 右键点击图片 -> 打开方式 -> 画图") print(f" 2. 将鼠标移动到目标位置,状态栏会显示坐标 (X, Y)") print(f" 3. 记录左上角坐标 (x1, y1) 和右下角坐标 (x2, y2)") print(f"\n 方法2: 使用Python脚本交互式查看") print(f" 运行: python {__file__} interactive") print(f"\n 方法3: 使用代码获取图片尺寸") print(f" 当前图片尺寸: {width} x {height}") print(f" 你可以根据这个尺寸估算坐标") # 如果是交互模式,尝试使用 tkinter 显示图片 if len(sys.argv) > 1 and sys.argv[1] == 'interactive': try: import tkinter as tk from PIL import ImageTk root = tk.Tk() root.title(f"坐标查看器 - {os.path.basename(image_path)}") # 计算显示尺寸(如果图片太大,缩放显示) max_display_size = 1200 display_width, display_height = width, height if width > max_display_size or height > max_display_size: scale = min(max_display_size / width, max_display_size / height) display_width = int(width * scale) display_height = int(height * scale) img_resized = img.resize((display_width, display_height), Image.Resampling.LANCZOS) photo = ImageTk.PhotoImage(img_resized) label = tk.Label(root, image=photo) label.pack() coord_label = tk.Label(root, text="将鼠标移动到图片上查看坐标", font=("Arial", 12)) coord_label.pack() def on_mouse_move(event): # 将显示坐标转换为实际坐标 if display_width != width or display_height != height: scale_x = width / display_width scale_y = height / display_height actual_x = int(event.x * scale_x) actual_y = int(event.y * scale_y) else: actual_x = event.x actual_y = event.y coord_label.config(text=f"坐标: ({actual_x}, {actual_y}) | 显示: ({event.x}, {event.y})") def on_click(event): # 将显示坐标转换为实际坐标 if display_width != width or display_height != height: scale_x = width / display_width scale_y = height / display_height actual_x = int(event.x * scale_x) actual_y = int(event.y * scale_y) else: actual_x = event.x actual_y = event.y print(f"点击坐标: ({actual_x}, {actual_y})") coord_label.config(text=f"点击坐标: ({actual_x}, {actual_y})", fg="red") label.bind("", on_mouse_move) label.bind("", on_click) print(f"\n交互窗口已打开,你可以:") print(f" - 移动鼠标查看坐标") print(f" - 点击鼠标记录坐标") print(f" - 关闭窗口退出") root.mainloop() except ImportError: print("\n提示: 安装 tkinter 可以启用交互模式") print(" Windows通常自带,如果缺失可以安装 python-tk 包") except Exception as e: print(f"打开图片失败: {e}") if __name__ == "__main__": # 默认查看 screenshot.png base_dir = os.path.dirname(os.path.abspath(__file__)) screenshot_path = os.path.join(base_dir, 'pic', 'screenshot.png') if len(sys.argv) > 1 and sys.argv[1] != 'interactive': # 如果提供了文件路径 image_path = sys.argv[1] else: image_path = screenshot_path show_image_coordinates(image_path)