|
|
@@ -7,6 +7,33 @@ import cv2
|
|
|
import numpy as np
|
|
|
from pathlib import Path
|
|
|
from typing import Tuple, Optional
|
|
|
+import os
|
|
|
+
|
|
|
+
|
|
|
+def read_image_with_unicode_path(image_path):
|
|
|
+ """
|
|
|
+ 读取包含 Unicode 字符(如中文)的图片路径
|
|
|
+ OpenCV 的 imread 在 Windows 上可能无法直接处理中文路径
|
|
|
+ 直接使用 imdecode 方法,避免 imread 的警告
|
|
|
+ """
|
|
|
+ # 将路径转换为绝对路径并规范化
|
|
|
+ abs_path = os.path.abspath(str(image_path))
|
|
|
+
|
|
|
+ # 直接使用文件读取 + imdecode,避免 imread 在中文路径上的问题
|
|
|
+ # 这样可以避免 cv2.imread 输出的警告信息
|
|
|
+ try:
|
|
|
+ with open(abs_path, 'rb') as f:
|
|
|
+ image_data = f.read()
|
|
|
+ # 将字节流解码为图片
|
|
|
+ img_array = np.frombuffer(image_data, np.uint8)
|
|
|
+ img = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
|
|
|
+ if img is None:
|
|
|
+ raise ValueError(f"cv2.imdecode 无法解码图片: {abs_path}")
|
|
|
+ return img
|
|
|
+ except FileNotFoundError:
|
|
|
+ raise FileNotFoundError(f"图片文件不存在: {abs_path}")
|
|
|
+ except Exception as e:
|
|
|
+ raise ValueError(f"无法读取图片: {abs_path}, 错误: {e}")
|
|
|
|
|
|
|
|
|
def match_image(
|
|
|
@@ -35,14 +62,9 @@ def match_image(
|
|
|
if not img2_path.exists():
|
|
|
raise FileNotFoundError(f"图片2不存在: {image2_path}")
|
|
|
|
|
|
- # 读取图片
|
|
|
- img1 = cv2.imread(str(img1_path))
|
|
|
- img2 = cv2.imread(str(img2_path))
|
|
|
-
|
|
|
- if img1 is None:
|
|
|
- raise ValueError(f"无法读取图片1: {image1_path}")
|
|
|
- if img2 is None:
|
|
|
- raise ValueError(f"无法读取图片2: {image2_path}")
|
|
|
+ # 读取图片(使用支持中文路径的方法)
|
|
|
+ img1 = read_image_with_unicode_path(img1_path)
|
|
|
+ img2 = read_image_with_unicode_path(img2_path)
|
|
|
|
|
|
# 如果提供了图片1的尺寸,则缩放图片1
|
|
|
if image1_size is not None:
|
|
|
@@ -109,14 +131,9 @@ def match_image_multiple(
|
|
|
if not img2_path.exists():
|
|
|
raise FileNotFoundError(f"图片2不存在: {image2_path}")
|
|
|
|
|
|
- # 读取图片
|
|
|
- img1 = cv2.imread(str(img1_path))
|
|
|
- img2 = cv2.imread(str(img2_path))
|
|
|
-
|
|
|
- if img1 is None:
|
|
|
- raise ValueError(f"无法读取图片1: {image1_path}")
|
|
|
- if img2 is None:
|
|
|
- raise ValueError(f"无法读取图片2: {image2_path}")
|
|
|
+ # 读取图片(使用支持中文路径的方法)
|
|
|
+ img1 = read_image_with_unicode_path(img1_path)
|
|
|
+ img2 = read_image_with_unicode_path(img2_path)
|
|
|
|
|
|
# 如果提供了图片1的尺寸,则缩放图片1
|
|
|
if image1_size is not None:
|
|
|
@@ -170,14 +187,32 @@ def match_image_multiple(
|
|
|
if __name__ == "__main__":
|
|
|
# 测试示例
|
|
|
import sys
|
|
|
+ import os
|
|
|
+
|
|
|
+ # 设置 UTF-8 编码,确保能正确处理中文路径
|
|
|
+ if sys.platform == 'win32':
|
|
|
+ import codecs
|
|
|
+ sys.stdout = codecs.getwriter('utf-8')(sys.stdout.buffer, 'strict')
|
|
|
+ sys.stderr = codecs.getwriter('utf-8')(sys.stderr.buffer, 'strict')
|
|
|
|
|
|
if len(sys.argv) < 3:
|
|
|
print("用法: python img-reg.py <图片1路径> <图片2路径> [图片1宽度] [图片1高度]")
|
|
|
print("示例: python img-reg.py screenshot.png template.png 1920 1080")
|
|
|
sys.exit(1)
|
|
|
|
|
|
- img1_path = sys.argv[1]
|
|
|
- img2_path = sys.argv[2]
|
|
|
+ # 处理路径:将正斜杠转换回反斜杠(Windows),并确保路径正确
|
|
|
+ img1_path = sys.argv[1].replace('/', os.sep)
|
|
|
+ img2_path = sys.argv[2].replace('/', os.sep)
|
|
|
+
|
|
|
+ # 确保路径是绝对路径,并规范化路径
|
|
|
+ if not os.path.isabs(img1_path):
|
|
|
+ img1_path = os.path.abspath(img1_path)
|
|
|
+ if not os.path.isabs(img2_path):
|
|
|
+ img2_path = os.path.abspath(img2_path)
|
|
|
+
|
|
|
+ # 规范化路径(移除多余的斜杠等)
|
|
|
+ img1_path = os.path.normpath(img1_path)
|
|
|
+ img2_path = os.path.normpath(img2_path)
|
|
|
|
|
|
image1_size = None
|
|
|
if len(sys.argv) >= 5:
|
|
|
@@ -188,6 +223,12 @@ if __name__ == "__main__":
|
|
|
except ValueError:
|
|
|
print("警告: 无法解析图片1尺寸,将使用原始尺寸")
|
|
|
|
|
|
+ # 验证文件是否存在
|
|
|
+ if not os.path.exists(img1_path):
|
|
|
+ raise FileNotFoundError(f"图片1不存在: {img1_path}")
|
|
|
+ if not os.path.exists(img2_path):
|
|
|
+ raise FileNotFoundError(f"图片2不存在: {img2_path}")
|
|
|
+
|
|
|
try:
|
|
|
result = match_image(img1_path, img2_path, image1_size)
|
|
|
if result:
|