""" 分析 diff 图的像素差异分布,按纵向区域统计差异密度。 输出每 50px 逻辑高度段的差异百分比,帮助定位需要调整的区域。 """ from pathlib import Path from PIL import Image import struct SCREENSHOTS_DIR = Path(__file__).resolve().parents[2] / "docs" / "h5_ui" / "screenshots" def analyze_diff_image(diff_path: Path, band_height: int = 150): """按纵向条带分析 diff 图的红色像素密度""" img = Image.open(diff_path).convert("RGB") w, h = img.size pixels = img.load() print(f"Diff 图尺寸: {w}×{h}") print(f"条带高度: {band_height}px (逻辑 {band_height/3:.0f}px)") print(f"{'区域':>8} {'逻辑Y':>8} {'差异像素':>10} {'总像素':>10} {'差异%':>8} {'条形图'}") print("-" * 80) bands = [] for y_start in range(0, h, band_height): y_end = min(y_start + band_height, h) diff_count = 0 total = 0 for y in range(y_start, y_end): for x in range(w): r, g, b = pixels[x, y] total += 1 # diff 图中红色/品红色像素表示差异 if r > 200 and g < 100: diff_count += 1 pct = (diff_count / total * 100) if total > 0 else 0 bar = "█" * int(pct / 2) logical_y = y_start / 3 print(f"{y_start:>6}-{y_end:<6} {logical_y:>6.0f}px {diff_count:>10,} {total:>10,} {pct:>7.1f}% {bar}") bands.append({"y_start": y_start, "y_end": y_end, "diff_pct": pct}) # 找出差异最大的区域 top_bands = sorted(bands, key=lambda b: b["diff_pct"], reverse=True)[:5] print(f"\n差异最大的 5 个区域:") for b in top_bands: logical_y = b["y_start"] / 3 print(f" 逻辑 Y={logical_y:.0f}px: {b['diff_pct']:.1f}%") def main(): diff_path = SCREENSHOTS_DIR / "diff-board-finance-v2.png" if not diff_path.exists(): print(f"❌ diff 图不存在: {diff_path}") return analyze_diff_image(diff_path, band_height=150) if __name__ == "__main__": main()