Files
SignatureSystem/image_test/compare_project_vs_test.py
2026-07-20 13:16:17 +08:00

83 lines
3.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""对比"项目实际产出的处理图" vs "测试脚本产出的 final.png"
使用前置条件:
1. 已经在前端跑过一次签章image_test/output/project_run/ 下有 _2_processed.png
2. 已经跑过 image_test/process.pyimage_test/output/test1/final.png 存在
输出:
逐文件对比尺寸/字节/SHA256给出"完全一致 / 仅尺寸相同 / 完全不同"的判定。
"""
import sys
import hashlib
from pathlib import Path
from PIL import Image, ImageChops
BASE_DIR = Path(__file__).resolve().parent
PROJECT_RUN_DIR = BASE_DIR / "output" / "project_run"
TEST_FINAL = BASE_DIR / "output" / "test1" / "final.png"
def stats(data):
img = Image.open(__import__('io').BytesIO(data))
if img.mode != 'RGBA':
img = img.convert('RGBA')
r, g, b, a = img.split()
luma = Image.merge('RGB', (r, g, b)).convert('L')
am = a.point(lambda p: 255 if p > 30 else 0)
lm = luma.point(lambda p: 255 if p < 210 else 0)
combined = ImageChops.darker(am, lm)
total = img.size[0] * img.size[1] or 1
return {
'size': img.size,
'bbox': combined.getbbox(),
'content_pct': 100.0 * sum(combined.histogram()[1:]) / total,
'sha': hashlib.sha256(data).hexdigest()[:12],
'bytes': len(data),
}
def main():
sys.stdout.reconfigure(encoding='utf-8')
if not TEST_FINAL.exists():
print(f"缺少基准文件: {TEST_FINAL}")
print("请先运行: python image_test/process.py")
sys.exit(1)
if not PROJECT_RUN_DIR.exists():
print(f"缺少项目运行输出目录: {PROJECT_RUN_DIR}")
print("请先在前端跑一次签章(已重启服务)")
sys.exit(1)
processed_files = sorted(PROJECT_RUN_DIR.glob("*_2_processed.png"))
if not processed_files:
print(f"{PROJECT_RUN_DIR} 下未找到 *_2_processed.png 文件")
sys.exit(1)
final_data = TEST_FINAL.read_bytes()
final_s = stats(final_data)
print(f"=== 基准: {TEST_FINAL.name} ===")
print(f" 尺寸={final_s['size']} bbox={final_s['bbox']} "
f"内容={final_s['content_pct']:.1f}% bytes={final_s['bytes']} sha={final_s['sha']}")
print()
print(f"=== 找到 {len(processed_files)} 个项目输出,逐个对比 ===\n")
for p in processed_files:
data = p.read_bytes()
s = stats(data)
identical = (data == final_data)
same_size = (s['size'] == final_s['size'])
print(f"[{p.name}]")
print(f" 尺寸={s['size']} bbox={s['bbox']} 内容={s['content_pct']:.1f}% "
f"bytes={s['bytes']} sha={s['sha']}")
if identical:
print(f" 判定: ✓ 与 final.png 逐字节完全一致")
elif same_size:
print(f" 判定: △ 尺寸相同但字节不同(可能只是 PNG 编码差异,目视应该一样)")
else:
print(f" 判定: ✗ 与 final.png 不同!尺寸={s['size']} vs {final_s['size']}")
print()
if __name__ == '__main__':
main()