Files
STM32F4-Base/simulator/tests/regen_ref_png.py
T
2026-09-10 11:36:33 +08:00

71 lines
2.8 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.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
regen_ref_png.py — 由黄金参考 .bin 批量重生成目检用 PNG(simulator/tests/ref/*.png)。
背景:ref/*.png 是本地目检调试产物(.gitignore 忽略),**不会**随 .bin 参考自动更新。
刷过 `check_content_pages.py --update` 或改了字体/视觉后,必须重跑本脚本,否则 PNG
仍是旧渲染(缺字/错位,看似"乱码")。
用法(仓库根目录;PIL 依赖用 .venv,见 AGENTS.md「Python 环境」):
.venv\\Scripts\\python.exe simulator/tests/regen_ref_png.py
.venv\\Scripts\\python.exe simulator/tests/regen_ref_png.py --scale 4
.venv\\Scripts\\python.exe simulator/tests/regen_ref_png.py --no-invert # 白字黑底(设备原样)
默认 `--scale 6 --invert`(深字白底,便于目检)。
退出码:0 全部成功;1 无 .bin 参考 / 有文件转换失败。
"""
import argparse
import importlib.util
import sys
from pathlib import Path
HERE = Path(__file__).resolve().parent # simulator/tests/
REF_DIR = HERE / "ref"
I1_TO_PNG = HERE.parent / "i1_to_png.py" # simulator/i1_to_png.py
def _load_i1_to_png():
"""从 simulator/i1_to_png.py 载入转换模块(目录不同,不能直接 import)。"""
spec = importlib.util.spec_from_file_location("i1_to_png", I1_TO_PNG)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod
def main():
ap = argparse.ArgumentParser(description="由 ref/*.bin 批量重生成目检 PNG")
ap.add_argument("--scale", type=int, default=6, help="放大倍数(默认 6)")
ap.add_argument("--invert", dest="invert", action="store_true", default=True,
help="前景/背景互换:深字白底(默认开,便于目检)")
ap.add_argument("--no-invert", dest="invert", action="store_false",
help="关闭互换:白字黑底(与设备显示一致)")
args = ap.parse_args()
if not I1_TO_PNG.exists():
print("FAIL: 找不到 %s" % I1_TO_PNG)
return 1
bins = sorted(REF_DIR.glob("*.bin"))
if not bins:
print("FAIL: %s 下没有 .bin 参考(先运行 check_content_pages.py --update)" % REF_DIR)
return 1
i1_to_png = _load_i1_to_png()
ok = 0
for b in bins:
try:
out = i1_to_png.i1_to_png(str(b), scale=args.scale, invert=args.invert)
print(" %-24s -> %s" % (b.name, Path(out).name))
ok += 1
except Exception as e: # 单文件失败不中断其余
print(" %-24s FAIL: %s" % (b.name, e))
print("%s: %d/%d 张 PNG 已生成(scale=%d invert=%s)"
% ("PASS" if ok == len(bins) else "FAIL", ok, len(bins), args.scale, args.invert))
return 0 if ok == len(bins) else 1
if __name__ == "__main__":
sys.exit(main())