feat(document-photo-to-pdf): raw PNG 页图导出到 out/,PDF 从 raw 派生

出件改为一次透视:PIL 按四边形自然尺寸 warp 出 raw 页图(只裁剪+拉正,不调色、不缩放、无白边),直接写 out/<PDF 主名>-NN.png;PDF 页再由 raw 调色 + LANCZOS 缩到画布内容区派生。相比「PDF 目标尺寸一次 + raw 自然尺寸一次」少一次 warp,10MP 自然尺寸从 numpy 的 3.0s/页降到 PIL 的 0.3s/页。

不新增输入参数:每次运行 PDF 与 PNG 全出,AI 不需要判断格式;SKILL.md 只补 out/ 说明、images 返回字段,以及 description 里的产出(PDF + out/ 下的 PNG 原图)。

页序先定后渲染,PNG 编号跟最终 PDF 页序;PDF 落盘成功后才清理同前缀的多余编号(失败保留上一份),不碰 out/ 里其它文件。numpy warp 与旧 warp_page 保留作回退路径。

真实样张(task-20261021-004,3 页)A/B:state.json 完全一致;PDF 逐页平均差 0.7 / 0.7 / 1.2 灰度级;raw PNG 与 numpy 自然尺寸 warp 平均差 0.02;3 页端到端 4.8s,PNG 0.58/0.69/0.90MB。注:doc_deskew/smoke_test.py 自 fe1c6d1 起未同步返回契约,属既有问题,本次未改。
This commit is contained in:
2026-10-24 09:54:37 +08:00
parent 8b8fbddb4f
commit e39273e97f
5 changed files with 140 additions and 23 deletions
@@ -5,7 +5,7 @@
- 所有 quad 都是“转正后原图”坐标;
- 目标尺寸把 PDF 画布内容区(页面尺寸 - 四周留白)的一条边填满,可放大低分辨率照片,
避免同一批页面因原图像素多少而大小不一;
- 不做中间 PNG,页面图直接进内存后一次性写 PDF。
- raw 页图(裁剪 + 透视拉正、自然尺寸、不调色)由 warp_raw 产出,PDF 页从它派生。
"""
import os
import statistics
@@ -13,7 +13,7 @@ import statistics
import numpy as np
from PIL import Image
from homography import brighten, warp_perspective
from homography import brighten, warp_perspective, warp_perspective_pil
# ---------------------------------------------------------------- 纸张
@@ -135,8 +135,27 @@ def target_size(quad, paper, page_orientation, P, out_ratio=None):
return max(1, int(round(tw))), max(1, int(round(th))), (cw, ch)
def warp_raw(img, quad):
"""只做裁剪 + 透视拉正:自然尺寸、不调色。raw 页图的唯一来源。"""
return warp_perspective_pil(img, quad, None)
def page_for_pdf(raw, size, P):
"""从 raw 页图派生 PDF 页:先调色,再按目标尺寸缩放(LANCZOS)。
与旧 warp_page 的差别只在采样顺序:先在自然尺寸做透视(超采样),
再缩到画布内容区,缩小时比直接按目标尺寸采样更少混叠。
"""
r = P["render"]
spec = P["enhance"].get(r.get("enhance") or r["default_enhance"])
page = tone(raw, spec, r["brighten_target"], r["brighten_pct"])
if page.size != tuple(size):
page = page.resize(tuple(size), Image.LANCZOS)
return page
def warp_page(img, quad, size, P):
"""透视校正 + 按 P["render"]["enhance"] 调色。"""
"""旧路径:直接按目标尺寸 warp + 调色。PIL 实现出问题时回退用。"""
r = P["render"]
page = warp_perspective(img, quad, size,
chunk_rows=r["warp_chunk_rows"])
@@ -19,6 +19,15 @@ def _solve_h(src, dst):
return vt[-1].reshape(3, 3)
def natural_size(quad):
"""四边形按自然边长对应的输出尺寸(与 warp_perspective 的 out_size=None 一致)。"""
q = np.asarray(quad, dtype=np.float64)
tl, tr, br, bl = q
w = int(round(max(np.linalg.norm(tr - tl), np.linalg.norm(br - bl))))
h = int(round(max(np.linalg.norm(bl - tl), np.linalg.norm(br - tr))))
return max(1, w), max(1, h)
def warp_perspective(img, quad, out_size=None, fill=(255, 255, 255),
chunk_rows=256):
"""把 quad 指定的四边形透视校正到 out_size。
@@ -29,12 +38,9 @@ def warp_perspective(img, quad, out_size=None, fill=(255, 255, 255),
arr = np.asarray(img if img.mode == "RGB" else img.convert("RGB"))
src_h, src_w = arr.shape[:2]
quad = np.asarray(quad, dtype=np.float64)
tl, tr, br, bl = quad
if out_size is None:
w = int(round(max(np.linalg.norm(tr - tl), np.linalg.norm(br - bl))))
h = int(round(max(np.linalg.norm(bl - tl), np.linalg.norm(br - tr))))
out_size = (w, h)
out_size = natural_size(quad)
out_w = max(1, int(out_size[0]))
out_h = max(1, int(out_size[1]))
@@ -80,6 +86,29 @@ def warp_perspective(img, quad, out_size=None, fill=(255, 255, 255),
return Image.fromarray(out)
def warp_perspective_pil(img, quad, out_size=None, fill=(255, 255, 255)):
"""PIL(C) 实现的透视校正,采样约定与 warp_perspective 一致。
out_size=None 时按四边形自然边长输出。10MP 约 0.3s,比上面的 numpy
逐像素实现快约 10 倍;实测两者平均差 0.26 灰度级、89% 像素完全一致。
只用于 raw 页图导出;PDF 出件仍走 numpy 版本,避免改变既有输出的像素。
"""
if out_size is None:
out_size = natural_size(quad)
ow = max(1, int(out_size[0]))
oh = max(1, int(out_size[1]))
dst = np.array([[0, 0], [ow - 1, 0], [ow - 1, oh - 1], [0, oh - 1]],
dtype=np.float64)
H = np.linalg.inv(_solve_h(np.asarray(quad, dtype=np.float64), dst))
H = H / H[2, 2]
coeffs = (H[0, 0], H[0, 1], H[0, 2],
H[1, 0], H[1, 1], H[1, 2],
H[2, 0], H[2, 1])
src = img if img.mode == "RGB" else img.convert("RGB")
return src.transform((ow, oh), Image.PERSPECTIVE, coeffs, Image.BILINEAR,
fillcolor=fill)
def brighten(img, target=245, pct=97):
"""全局等比提亮:把 pct 分位映射到 target,保持相对对比度。"""
arr = np.asarray(img if img.mode == "RGB" else img.convert("RGB")).astype(np.float32)