Skip to main content

kindle-pdf-cropper

自动剪裁 PDF 四周白边与页眉/页脚、为 Kindle 生成阅读优化 PDF/EPUB 的命令行工具。

  • 剪掉每页四周白边 + 页眉/页脚 + 页码,只保留正文内容;
  • 可选 --split:把每页上下切成两页,输出横版页面,Kindle 横屏阅读字号接近翻倍;
    • 默认(自适应切分):不指定重叠参数时,程序自动把切线放在两行文字之间的空白处—— 文字型 PDF 用文本行 bbox 找行间空隙,扫描型 PDF 用图像识别算法找两行之间的空白带—— 因此不会切断任何一行,也不需要重叠区域;
    • 也可以显式指定 --split-overlap-* 恢复"中线切分 + 两页重叠"的旧模式;
  • 自适应文字型与扫描图片型 PDF;输出结果保持原样——文字仍是文字、 扫描图仍是扫描图,文字/公式/表格的排版完全不变,只做整页剪裁与切分;
  • 输出 PDF(文字/矢量/图片保真)或 EPUB(固定版式,排版不重排)。

技术选型调研见 docs/research.md,算法设计见 docs/design.md。

安装

要求 Python ≥ 3.10。

cd kindle-pdf-cropper
python -m venv .venv
.venv\Scripts\activate            # Windows
# source .venv/bin/activate       # Linux / macOS
pip install -e .

快速开始

# 只裁白边和页眉页脚,输出 PDF(默认)
kindle-crop 书.pdf

# 裁切 + 上下切分成横版两页(自适应:切线自动落在两行文字之间的空白处,不切行)
kindle-crop 书.pdf --split -o out/书_横版

# 旧模式:中线切分 + 两页重叠(需要指定重叠参数)
kindle-crop 书.pdf --split --split-overlap-pt 20 -o out/书_横版

# 同时输出 PDF 和 EPUB(自动:文字页=SVG,扫描页=位图)
kindle-crop 书.pdf --format both -o out/书

# 扫描件:调大安全边距、提高 EPUB 位图质量
kindle-crop 扫描.pdf --padding 10 --epub-dpi 220 --format epub

# 调试:把每页的分析叠加图(裁剪框/页眉页脚带/切分框)导出成 PNG
kindle-crop 书.pdf --debug-dir debug

常用参数

参数 默认 说明
--format pdf|epub|both pdf 输出格式
--split 关 每页上下切成两页横版
--split-overlap-pt —(自适应) 指定后启用"中线切分+重叠":两页最小重叠(点)
--split-overlap-ratio —(自适应) 指定后启用"中线切分+重叠":重叠 = max(比例×裁剪高, 最小点数)
--padding 6 内容框外保留的安全边距(点)
--no-header-footer 关 只裁边距,不删除页眉/页脚/页码
--no-watermark-removal 关 关闭扫描页水印/重复图案的自动去除
--epub-mode image|svg|auto auto 文字页→SVG,扫描页→位图
--epub-dpi 200 EPUB 位图分辨率
--epub-image-format jpeg|png jpeg EPUB 位图格式
--analysis-dpi 144 分析用渲染分辨率
--debug-dir DIR — 导出每页分析叠加图
-o PATH 输入名+_cropped 输出路径(both 时自动补 .pdf/.epub)

自适应切分说明(--split)

不指定 --split-overlap-* 时,切分线按如下方式自动确定:

  1. 文字型 PDF:取裁剪区内所有文本行 bbox,找出相邻两行之间宽度足够的空白间隙 (≥ 0.4 × 中位行高),把切线放在该间隙正中——上一行完整保留在上半页, 下一行完整保留在下半页,中间无重叠也不丢内容;
  2. 扫描型 PDF:用图像识别算法对裁剪区做逐像素行投影,找到连续 "无墨水行"的空白带(行间空白即使只有几像素宽也能分辨),要求 切线行及其上下 1px 均为纯空白,把切线放在空白带正中;被两侧实心 墨块夹住的空隙(图/大公式内部)优先级最低——只要别处有文字间隙, 就不会把一张图切成两半;
  3. 若整页几乎找不到可用空白带(如旋转 90° 的扫描页),退化为中线切分, 保证上下两页尺寸均衡。

指定 --split-overlap-* 后恢复旧模式:在中线切分,两页保留 max(比例×裁剪高, 最小点数) 的重叠,保证中线附近的一行不会显示不全。

水印 / 重复图案去除(扫描页自动)

许多扫描版书籍带有浅色水印(如对角重复的站点文字、页脚图案、扫描边缘带), 它们出现在每一页的相同位置,会干扰页眉/页脚判定(例如把页脚区域和正文合并成 一个大色带,导致页眉页脚都裁不掉)。

程序默认自动检测并去除这类重复图案:

  1. 在全书中均匀抽样(最多 40 页),对每个像素计算跨页中位灰度;
  2. 某像素在多数页面上恒定比纸张更暗(中位数低于背景阈值),而正文像素在 多数页面是背景色 → 该像素属于水印/页眉/页框/扫描边缘等重复图案;
  3. 把这些像素从分析用的墨水掩码中剔除(正文像素基本不受影响), 再执行页眉/页脚检测与裁剪——页脚区域不再被水印"糊住";
  4. 保护措施:若某页剔除后内容不足原墨迹的 35%,则保留原样(防止把 "每页相同的表格/插图"误当水印删掉);文字型(born-digital)PDF 不启用。

如需关闭:kindle-crop 书.pdf --no-watermark-removal。

输出说明

  • PDF 输出:每个输出页的页面尺寸就是裁剪后的尺寸(物理裁剪,而非仅改 CropBox, 任何阅读器都稳定显示);文字保持可选中的文字,表格线/公式等矢量、嵌入字体原样保留; 目录与元数据保留。
  • EPUB 输出:EPUB 是流式重排容器,若要保持排版不变只能用固定版式: 每页钉死一个 viewport。
    • 文字页 → 内联 SVG(文字仍为文字,矢量图形保留);
    • 扫描页 → 整页 JPEG/PNG 位图;
    • 若你的阅读器对 SVG/固定版式支持不佳(如部分 Kindle 固件),请用 --epub-mode image 或直接使用 PDF 输出。

示例文件

examples/ 下有生成的样例(文字型、扫描型、旋转扫描型): 运行 python tests/make_samples.py text examples/sample_text.pdf 等重新生成。

开发与测试

pip install -e .[dev]
python -m pytest

测试覆盖:边距/页眉页脚分析、扫描与旋转页、PDF 输出文字保真、切分重叠、 自适应切分(文字行间 / 扫描空白带 / 旋转页退避)、EPUB 结构与内容校验、CLI 端到端。

许可

MIT,见 LICENSE。

Metadata

Release files for kindle-pdf-cropper 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kindle-pdf-cropper 0.1.2
File Size Uploaded Provenance Actions
kindle_pdf_cropper-0.1.2.tar.gz 35.2 kB With provenance Details for kindle_pdf_cropper-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for kindle-pdf-cropper 0.1.2
File Interpreter ABI Platform Actions
kindle_pdf_cropper-0.1.2-py3-none-any.whl Python 3 none any Details for kindle_pdf_cropper-0.1.2-py3-none-any.whl

Total release size: 65.0 kB

Release files / kindle_pdf_cropper-0.1.2.tar.gz

Download URL kindle_pdf_cropper-0.1.2.tar.gz
Size 35.2 kB
Tags Source
SHA-256 checksum
How to use checksums
51c664db6346d387519d0ae891d602098b4e15f7abded476e26c0f52e5b9aaca
BLAKE2b-256 checksum
How to use checksums
c15c39be1143db9b6e1df03bd85fd554c2613e4912b52b4ded0c344e868f73fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 30, 2026.

Transparency log

Release files / kindle_pdf_cropper-0.1.2-py3-none-any.whl

Download URL kindle_pdf_cropper-0.1.2-py3-none-any.whl
Size 29.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eb6ac13655453376ae28a5e6e7fa6a86da05d54abc2591a6cf1b75369899a7cf
BLAKE2b-256 checksum
How to use checksums
a35babd955bf3beca068e3a4cf3479d0863adab77640f88a19f4620925344b95
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 30, 2026.

Transparency log

Security for kindle-pdf-cropper 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for kindle_pdf_cropper-0.1.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 30, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page