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

98 lines
3.3 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.
"""穷举 process_image 的参数组合,找出哪种组合会产出"未紧致裁剪"的输出。
逻辑:
final.png (153x74, bbox=(0,0,153,74)) 是已知正确的紧致输出。
遍历 is_signature/height_cm 等参数,跑 process_image对比输出:
- 尺寸 == (153, 74)?
- bbox 是否贴满?
- 字节内容是否与 final.png 完全一致?
任何一项不一致 -> 找到 bug 触发条件。
输入图: image_test/input/test1.jpg
"""
import io
import sys
import hashlib
from pathlib import Path
from PIL import Image, ImageChops
BASE_DIR = Path(__file__).resolve().parent
PROJECT_ROOT = BASE_DIR.parent
INPUT_IMAGE = BASE_DIR / "input" / "test1.jpg"
FINAL_REF = BASE_DIR / "output" / "test1" / "final.png"
def _ensure_path():
if str(PROJECT_ROOT) not in sys.path:
sys.path.insert(0, str(PROJECT_ROOT))
def _stats(data):
img = Image.open(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():
_ensure_path()
sys.stdout.reconfigure(encoding='utf-8')
if not INPUT_IMAGE.exists():
print(f"缺少: {INPUT_IMAGE}")
sys.exit(1)
if not FINAL_REF.exists():
print(f"缺少: {FINAL_REF},请先运行 image_test/process.py 生成")
sys.exit(1)
with open(INPUT_IMAGE, 'rb') as f:
image_bytes = f.read()
with open(FINAL_REF, 'rb') as f:
final_bytes = f.read()
final_stats = _stats(final_bytes)
print(f"=== 参考基准 final.png ===")
print(f" 尺寸={final_stats['size']} bbox={final_stats['bbox']} "
f"内容={final_stats['content_pct']:.1f}% 字节={final_stats['bytes']} "
f"sha={final_stats['sha']}")
print()
from lib.image_processor import process_image
cases = [
# (label, kwargs)
("is_sig=True noise=0 rot=0", {'is_signature': True}),
("is_sig=False noise=0 rot=0", {'is_signature': False}),
("is_sig=True noise=3 rot=0", {'is_signature': True, 'noise_level': 3}),
("is_sig=True noise=0 rot=-5,5",{'is_signature': True, 'rotate_min': -5, 'rotate_max': 5}),
("is_sig=True noise=3 rot=-5,5",{'is_signature': True, 'noise_level': 3, 'rotate_min': -5, 'rotate_max': 5}),
]
print(f"=== 穷举 {len(cases)} 种参数组合 ===\n")
for label, kw in cases:
out = process_image(image_bytes, log_source='测试', **kw)
s = _stats(out)
tight = (s['bbox'] == (0, 0, s['size'][0], s['size'][1]))
same_as_final = (out == final_bytes)
flag = "✓紧致" if tight else "✗有padding"
flag2 = "=final" if same_as_final else "≠final"
print(f"[{label}]")
print(f" 尺寸={s['size']} bbox={s['bbox']} 内容={s['content_pct']:.1f}% "
f"字节={s['bytes']} {flag} {flag2}")
if __name__ == '__main__':
main()