nex_animation/reports/ui_evidence/nex-p19/pxdiff.py

45 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""阶段 19 · 修复前后像素比对:污染态 CSS(88 997 B)vs 干净态 CSS(77 786 B)。
nex-p18/visualcheck/shots = 阶段 18 那次 11 步全绿回归留下的 90 张(当时 dist-b1 在扫描集里)
nex-p19/visualcheck/shots = 本轮加 @source not 之后的同一套 90 态
比法:先 sha256 逐字节;不等的再用 PIL 逐像素数差异像素与最大通道差。
注意:截图里有相对时间(「3 分钟前」)、生成中进度、动态徽标,
逐字节不等 ≠ 视觉回归 —— 那种图要给出差异面积与位置,再人工判定是否 CSS 造成。
"""
import hashlib, os, sys
from PIL import Image, ImageChops
A = sys.argv[1] if len(sys.argv) > 1 else "reports/ui_evidence/nex-p18/visualcheck/shots"
B = sys.argv[2] if len(sys.argv) > 2 else "reports/ui_evidence/nex-p19/visualcheck/shots"
RA, RB = sorted(os.listdir(A)), sorted(os.listdir(B))
only_a, only_b = set(RA) - set(RB), set(RB) - set(RA)
same = diff = 0
rows = []
for f in RA:
if f not in set(RB):
continue
ba, bb = open(os.path.join(A, f), "rb").read(), open(os.path.join(B, f), "rb").read()
if hashlib.sha256(ba).hexdigest() == hashlib.sha256(bb).hexdigest():
same += 1
continue
ia, ib = Image.open(os.path.join(A, f)), Image.open(os.path.join(B, f))
if ia.size != ib.size:
rows.append((f, "size", ia.size, ib.size, None, None)); diff += 1; continue
d = ImageChops.difference(ia.convert("RGB"), ib.convert("RGB"))
bbox = d.getbbox()
npx = 0
if bbox:
px = d.crop(bbox).convert("RGB").getdata()
npx = sum(1 for p in px if p != (0, 0, 0))
mx = max(max(p) for p in px)
else:
mx = 0
area = (bbox[2] - bbox[0]) * (bbox[3] - bbox[1]) if bbox else 0
rows.append((f, "bytes", bbox, npx, mx, area)); diff += 1
print(f"A={A} {len(RA)} 张")
print(f"B={B} {len(RB)} 张")
print(f"仅 A 有 = {sorted(only_a)[:5]} 仅 B 有 = {sorted(only_b)[:5]}")
print(f"逐字节相同 = {same} / {len(set(RA) & set(RB))} 有差异 = {diff}")
for f, kind, bbox, npx, mx, area in rows:
tot = None
print(f" DIFF {f:<34} {kind:<6} bbox={bbox} 差异像素={npx} 最大通道差={mx} 包围盒面积={area}")