#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 企业微信资产包门禁(第五十二刀) 跑法:/usr/bin/python3 check-wecom.py 退出码分档(不是"红=1"一把梭):0 全绿 / 1 判红 / 2 门禁自身崩溃或夹具缺失 四条判据都配了**负对照**,负对照不判红就等于这条判据没有约束力: B 圆即底板 ← 现网 assets-delivery/icon-512x512.png(圆角方版)必须被判红 D 板内居中 ← 故意平移 6px 的副本必须被判红 E 朱砂面积带 ← 把红涂满整板的副本必须被判红 F 反白无彩 ← 给反白版塞一枚朱砂 A 的副本必须被判红 """ import os import re import sys import tempfile try: from PIL import Image except Exception as e: # 解释器选错会读成"依赖缺失",见记忆 reference-python-pillow-path print("FATAL 需要 Pillow,且必须用 /usr/bin/python3 跑:", e) sys.exit(2) HERE = os.path.dirname(os.path.abspath(__file__)) PKG = os.path.join(HERE, "assets-wecom") RED = (196, 30, 58) # #C41E3A ALPHA_EDGE = 8 # 视为"全透明"的上限 fails, oks, expected = [], [], [] def ok(msg): oks.append(msg) print(f" ok {msg}") def bad(msg): fails.append(msg) print(f" RED {msg}") def name_size(fn): m = re.search(r"(\d+)x(\d+)", fn) if m: return int(m.group(1)), int(m.group(2)) m = re.search(r"-(\d+)\.png$", fn) return (int(m.group(1)), int(m.group(1))) if m else (None, None) def is_red(px): return abs(px[0] - RED[0]) + abs(px[1] - RED[1]) + abs(px[2] - RED[2]) <= 30 def red_ratio(im): """朱砂像素占全板比例(%)。透明像素不算红。""" im = im.convert("RGBA") w, h = im.size px = im.load() n = 0 for y in range(h): for x in range(w): p = px[x, y] if p[3] > 128 and is_red(p): n += 1 return 100.0 * n / (w * h) def ink_mask(im): """返回 (mask, w, h)。mask[y][x]=1 表示该像素是"墨"。 白底板:非白即墨;透明底:不透明即墨。""" im = im.convert("RGBA") w, h = im.size px = im.load() opaque_white = 0 total = 0 mask = [] for y in range(h): row = [] for x in range(w): r, g, b, a = px[x, y] total += 1 if a <= ALPHA_EDGE: row.append(0) continue if r >= 245 and g >= 245 and b >= 245 and a >= 250: row.append(0) continue row.append(1) mask.append(row) return mask, w, h, px def bbox(mask, w, h): xs = [x for row in mask for x, v in enumerate(row) if v] ys = [y for y, row in enumerate(mask) if any(row)] if not xs or not ys: return None return min(xs), min(ys), max(xs), max(ys) def check_circle_lossless(path, label, must_fail=False): """内切圆**之外**的像素必须全透明 —— 头像版是"圆即底板",方版被圆裁掉的是底板四角。 ⚠ 这条**不是**"咬字"判据:实测两版白字最远极径都在内切圆内(余量 8.0% / 10.3%), 它判的是"这枚圆是被设计的,还是被裁出来的"。""" im = Image.open(path).convert("RGBA") w, h = im.size cx = cy = (w - 1) / 2.0 r = w / 2.0 px = im.load() viol = 0 for y in range(h): dy = y - cy for x in range(w): dx = x - cx if dx * dx + dy * dy > (r + 1.0) ** 2 and px[x, y][3] > ALPHA_EDGE: viol += 1 total = w * h if viol: msg = f"{label}: 内切圆外残留 {viol} 个不透明像素({100.0*viol/total:.2f}%)⇒ 圆裁会切掉底板四角,方版语义丢失" if must_fail: expected.append("B 圆角方版负对照: " + msg) ok("预期红(负对照被抓):" + msg) else: bad(msg) else: msg = f"{label}: 圆外零残留,圆即底板" (bad if must_fail else ok)(f"负对照未被抓 ⇒ B 条无约束力:{msg}" if must_fail else msg) def check_plate(path, label, must_fail=False): """must_fail=True 用于负对照:判红是**预期红**,登记进 expected 而不是计进失败数 (否则"闸抓得越准,退出码越红",正是记忆里那条退出码分档的反例)。""" mask, w, h, px = ink_mask(Image.open(path)) bb = bbox(mask, w, h) if not bb: (bad if not must_fail else ok)(f"{label}: 一个墨像素都没有") return x0, y0, x1, y1 = bb l, r, t, b = x0, w - 1 - x1, y0, h - 1 - y1 msg = f"{label}: 墨 {x1-x0+1}x{y1-y0+1} 左右差 {l-r}、上下差 {t-b}" if max(l, r) >= w or max(t, b) >= h: (ok if must_fail else bad)("预期红:墨越出板外 —— " + msg if must_fail else "墨越出板外 —— " + msg) return if abs(l - r) > 1 or abs(t - b) > 1: if must_fail: expected.append("E 平移负对照: " + msg) ok("预期红(负对照被抓):" + msg) else: bad("未居中 —— " + msg) else: if must_fail: bad(f"负对照未被抓 ⇒ E 条无约束力:{msg}") else: ok("居中 —— " + msg) def main(): if not os.path.isdir(PKG): print(f"FATAL 资产包缺失:{PKG}") return 2 import json rp = os.path.join(PKG, "pack-report.json") if not os.path.exists(rp): print(f"FATAL 装配报告缺失:{rp} ⇒ 先跑 wecom-pack.cjs,别拿半套包过闸") return 2 report = json.load(open(rp, encoding="utf-8")) files = sorted(f for f in os.listdir(PKG) if f.endswith(".png")) if len(files) < 14: print(f"FATAL 只找到 {len(files)} 张 PNG,少于 14 ⇒ 判据覆盖面不足") return 2 print(f"[A] IHDR 尺寸必须等于文件名声明({len(files)} 张)") for f in files: want = name_size(f) got = Image.open(os.path.join(PKG, f)).size (ok if got == want else bad)(f"{f}: 声明 {want[0]}x{want[1]} 实测 {got[0]}x{got[1]}") print("\n[A2] 装配报告登记的每张必须真在盘上(防「备好但从未跑成」)") declared, present = set(report.get("files", {})), set(files) missing, extra = sorted(declared - present), sorted(present - declared) (ok if not missing else bad)(f"登记 {len(declared)} 张,盘上缺 {len(missing)} 张" + "".join(f" [{m}]" for m in missing)) (ok if not extra else bad)(f"盘上未登记 {len(extra)} 张" + "".join(f" [{e}]" for e in extra)) print("\n[B] 头像:圆即底板(内切圆外不许有任何不透明像素)") for f in files: if f.startswith("wecom-avatar-"): check_circle_lossless(os.path.join(PKG, f), f) print(" —— 负对照(现网圆角方版,必须被判红)") check_circle_lossless(os.path.join(HERE, "assets-delivery", "icon-512x512.png"), "icon-512x512.png", must_fail=True) print("\n[C] 头像:中心是朱砂地(白文印的底不能丢)") for f in files: if f.startswith("wecom-avatar-"): im = Image.open(os.path.join(PKG, f)).convert("RGBA") c = im.load()[im.size[0] // 2, im.size[1] // 2] (ok if is_red(c) else bad)(f"{f}: 中心像素 {c[:3]}") print("\n[D] 头像:512 与 144 两档的朱砂占比必须同格(容差按抗锯齿的 1/尺寸律给,不假装它是 0)") for kind in ("full", "ring2"): ratios = {} for px_ in (512, 144): p = os.path.join(PKG, f"wecom-avatar-{kind}-{px_}.png") if os.path.exists(p): ratios[px_] = red_ratio(Image.open(p)) if len(ratios) == 2: d = abs(ratios[512] - ratios[144]) (ok if d <= 3.0 else bad)(f"{kind}: 512 占 {ratios[512]:.2f}% / 144 占 {ratios[144]:.2f}% 差 {d:.2f}pt(闸 3.0)") print(" —— 负对照(把圆面缩 30%,模拟被咬/缩水,必须被抓)") with tempfile.TemporaryDirectory() as td: im = Image.open(os.path.join(PKG, "wecom-avatar-full-512.png")).convert("RGBA") small = im.resize((358, 358), Image.LANCZOS) canvas = Image.new("RGBA", (512, 512), (0, 0, 0, 0)) canvas.paste(small, (77, 77), small) p2 = os.path.join(td, "wecom-avatar-shrunk-512.png") canvas.save(p2) base = red_ratio(Image.open(os.path.join(PKG, "wecom-avatar-full-512.png"))) shrunk = red_ratio(Image.open(p2)) d = abs(base - shrunk) (ok if d > 3.0 else bad)(f"负对照缩水后占比 {base:.2f}%→{shrunk:.2f}% 差 {d:.2f}pt ⇒ " + ("被抓" if d > 3.0 else "未被抓 ⇒ D 条对'面变小'不敏感")) print("\n[E] 企业logo 板:居中 + 尺寸") for f in files: if f.startswith("wecom-logo-"): check_plate(os.path.join(PKG, f), f) print(" —— 负对照(故意平移 6px,必须被判红)") with tempfile.TemporaryDirectory() as td: src = os.path.join(PKG, "wecom-logo-stacked-702x180-white.png") im = Image.open(src).convert("RGBA") canvas = Image.new("RGBA", im.size, (255, 255, 255, 255)) canvas.paste(im.crop((6, 0, im.size[0], im.size[1])), (0, 0)) shifted = os.path.join(td, "wecom-logo-shifted-702x180-white.png") canvas.save(shifted) check_plate(shifted, "shifted-6px(负对照)", must_fail=True) print("\n[F] 白底板:朱砂面积 —— 有红的版必须落在 0.1%–10%(R6 面积律,两端都要断);" "\n 源字标本身不含红的版(name 只走名、不含号 ⇒ 无 A 可加彩)断言恰为 0,不硬要它出红") src_red = {} for f, meta in report.get("files", {}).items(): s = os.path.join(HERE, meta.get("source", "")) src_red[f] = os.path.exists(s) and 'fill="#C41E3A"' in open(s, encoding="utf-8").read() for f in files: if f.endswith("-white.png"): ratio = red_ratio(Image.open(os.path.join(PKG, f))) if src_red.get(f): (ok if 0.1 <= ratio <= 10.0 else bad)(f"{f}: 朱砂 {ratio:.2f}%(源含红)") else: (ok if ratio == 0.0 else bad)(f"{f}: 朱砂 {ratio:.2f}%(源不含红 ⇒ 期望恰 0)") print(" —— 负对照(把红涂满整板,必须被判红)") with tempfile.TemporaryDirectory() as td: im = Image.open(os.path.join(PKG, "wecom-logo-stacked-702x180-white.png")).convert("RGBA") px = im.load() for y in range(im.size[1]): for x in range(im.size[0]): if px[x, y][3] > ALPHA_EDGE and not is_red(px[x, y]): px[x, y] = (RED[0], RED[1], RED[2], 255) p2 = os.path.join(td, "wecom-logo-allred-702x180-white.png") im.save(p2) ratio = red_ratio(Image.open(p2)) caught = not (0.1 <= ratio <= 10.0) (ok if caught else bad)(f"负对照全红 {ratio:.2f}% ⇒ " + ("被抓" if caught else "未被抓 ⇒ F 条无约束力")) print("\n[G] 反白版:0 朱砂(裁定「反白不加彩也不加粗」)") for f in files: if f.endswith("-reversed.png"): ratio = red_ratio(Image.open(os.path.join(PKG, f))) (ok if ratio == 0.0 else bad)(f"{f}: 朱砂 {ratio:.2f}%") print(" —— 负对照(给反白版塞一枚朱砂 A,必须被判红)") with tempfile.TemporaryDirectory() as td: im = Image.open(os.path.join(PKG, "wecom-logo-stacked-702x180-reversed.png")).convert("RGBA") px = im.load() n = 0 for y in range(im.size[1]): for x in range(im.size[0]): if px[x, y][3] > 128 and n < 400: px[x, y] = (RED[0], RED[1], RED[2], 255) n += 1 p2 = os.path.join(td, "wecom-logo-redstain-702x180-reversed.png") im.save(p2) ratio = red_ratio(Image.open(p2)) (ok if ratio > 0 else bad)(f"负对照染色 {ratio:.3f}% ⇒ " + ("被抓" if ratio > 0 else "未被抓 ⇒ G 条无约束力")) print("\n[H] 透明墨色版:不许出现纯白像素(底板没剥干净会留白块)") for f in files: if f.endswith("-transparent.png"): im = Image.open(os.path.join(PKG, f)).convert("RGBA") px = im.load() w, h = im.size n = sum( 1 for y in range(h) for x in range(w) if px[x, y][3] > 250 and px[x, y][0] >= 250 and px[x, y][1] >= 250 and px[x, y][2] >= 250 ) (ok if n == 0 else bad)(f"{f}: 纯白不透明像素 {n}") # 预期红必须有条数下限:一条都没有 = 负对照根本没跑成,那上面所有"ok"都失去自证 (ok if len(expected) >= 2 else bad)(f"预期红登记 {len(expected)} 条(负对照至少 2 条:B 圆角方版、E 平移)") print("\n预期红明细(负对照,不计失败):") for m in expected: print(" 预期红:", m) print(f"\n{'='*58}\n判红 {len(fails)} 项 / 通过 {len(oks)} 项") for m in fails: print(" RED:", m) return 1 if fails else 0 if __name__ == "__main__": try: sys.exit(main()) except SystemExit: raise except Exception: import traceback traceback.print_exc() print("EXIT=2 门禁自身崩溃(与判红不是同一件事,别混)") sys.exit(2)