_tmp_seal_size.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # -*- coding: utf-8 -*-
  2. """定标授权书扫描件:找纸张/版心边界,反推公章、印鉴、签名的真实毫米数。"""
  3. import io
  4. import os
  5. import sys
  6. import numpy as np
  7. from PIL import Image
  8. sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
  9. HERE = os.path.dirname(os.path.abspath(__file__))
  10. SRC = os.path.join(HERE, "_bid_assets", "seal_src", "p1_img1.png")
  11. SEAL = os.path.join(HERE, "_bid_assets", "seal")
  12. OUT = os.path.join(HERE, "_tmp_inspect")
  13. os.makedirs(OUT, exist_ok=True)
  14. im = Image.open(SRC).convert("RGB")
  15. a = np.asarray(im.convert("L"))
  16. h, w = a.shape
  17. print(f"扫描件 {w}×{h} px,宽高比 {w / h:.4f}"
  18. f"(A4 竖版应为 {210 / 297:.4f})")
  19. # 纸张边界:扫描仪底色通常明显暗于纸面
  20. row_med = np.median(a, axis=1)
  21. col_med = np.median(a, axis=0)
  22. print(f"行中位亮度:首 {row_med[:8].astype(int)} … 末 {row_med[-8:].astype(int)}")
  23. print(f"列中位亮度:首 {col_med[:8].astype(int)} … 末 {col_med[-8:].astype(int)}")
  24. # 版心(印刷内容)边界:按暗像素投影
  25. dark = a < 170
  26. cols = dark.sum(axis=0)
  27. rows = dark.sum(axis=1)
  28. thr_c, thr_r = max(6, h // 400), max(6, w // 400)
  29. xs = np.nonzero(cols > thr_c)[0]
  30. ys = np.nonzero(rows > thr_r)[0]
  31. print(f"\n内容范围 x[{xs.min()}, {xs.max()}] y[{ys.min()}, {ys.max()}]"
  32. f" 即 {xs.max() - xs.min() + 1}×{ys.max() - ys.min() + 1} px")
  33. # 假定纸张为 A4 且扫描件已裁到纸张边缘
  34. for label, paper_w in (("按整幅=A4宽210mm", 210.0),
  35. ("按整幅=A4高297mm定标", 297.0 * w / h)):
  36. mm_px = paper_w / w
  37. print(f"\n【{label}】1 px = {mm_px:.4f} mm → 整幅 "
  38. f"{w * mm_px:.0f}×{h * mm_px:.0f} mm")
  39. for name in ("gongzhang", "yinjian", "signature"):
  40. p = os.path.join(SEAL, f"{name}.png")
  41. if not os.path.exists(p):
  42. continue
  43. al = np.asarray(Image.open(p))[..., 3]
  44. ys2, xs2 = np.nonzero(al > 8)
  45. bw, bh = xs2.max() - xs2.min() + 1, ys2.max() - ys2.min() + 1
  46. print(f" {name:10s} {bw:4d}×{bh:4d} px = "
  47. f"{bw * mm_px:5.1f}×{bh * mm_px:5.1f} mm")
  48. im.resize((w // 3, h // 3), Image.LANCZOS).save(
  49. os.path.join(OUT, "seal_src_thumb.jpg"), quality=80)
  50. print(f"\n缩略图 → {os.path.join(OUT, 'seal_src_thumb.jpg')}")