Folio Fold
Folio Fold suggests where to split a photograph or scan of an open book into two facing pages. It returns a straight line that can follow an angled binding. Use the overlay to check where that line crosses the book before exporting either page. The CPU model is 703 KB and works offline after download.
The teal line is the prediction. Yellow dots mark three positions placed before scoring. Errors are the largest horizontal distance from a dot to the line, divided by the full photograph width. The photos are resized and annotated versions of works by PMRMaeyaert and 3s, both under CC BY-SA 4.0. Hymn book · Display case · Example image license
Run on one book
Install the wheel from this repository. It includes the 703 KB ONNX weights, the offline review page and its fonts. The command prints the fold endpoints as JSON and can draw an overlay on a new PNG.
With the Hugging Face CLI (hf) installed, download the model configuration and wheel installer. The installer reads the pinned wheel revision from config.json and pip checks its SHA-256 digest. For pip without the Hub CLI, use the versioned wheel.
hf download foliokestrel/folio-fold config.json install_wheel.py --local-dir folio-fold-install
python folio-fold-install/install_wheel.py
folio-fold book.jpg --overlay review.png
To use the tool without installing Python, open the sample and select Suggest fold. Try the book spread editor
For a batch, create one session and reuse it.
from PIL import Image
from folio_fold import FoldLocator
model = FoldLocator()
with Image.open("book.jpg") as book:
result = model.predict(book)
print(result["top_x"], result["bottom_x"])
The endpoints are fractions of the EXIF-oriented image width, measured from the left at the top and bottom of the whole image. At fractional height y, the line crosses at (1-y)*top_x + y*bottom_x. The result also includes displayed width and height in pixels and row_support, an uncalibrated measure of agreement between rows. Inputs should show one upright two-page spread. The model always proposes a line, including on single pages, backgrounds or curved bindings. A reviewer can place two points on the visible fold and export full-resolution pages in the Book spread editor.
The repository checkout also supports the original python locate.py, python batch_review.py and python pdf_review.py commands. To build from the checkout, run python -m pip install . or python -m pip install '.[pdf]' from its root.
Review a folder of scans
Prepare the suggestions once, open the review in a local browser, and save the pages you approve. The model, previews and decisions stay on your computer. The installed wheel includes the folder reviewer.
folio-fold-batch prepare ./scans ./review
Open review/index.html. Select a scan and inspect the green line. Clear Show cut line to inspect the crease underneath. Use scan center tries a vertical cut at 50% of the scan width. Reset proposal restores the model line. Try the two-spread offline example
To correct it, edit the top and bottom percentages or choose Mark two points and click near opposite ends of the visible binding. Approve cut exports the two halves of a spread. Keep whole page retains a cover, title page or single leaf as one PNG. Skip scan omits a scan. Each decision advances to the next pending scan. Editing an approved line returns it to pending.
Choose Weakest fold evidence first to start with scans whose rows gave the least support to the model’s proposed cut. This changes the inspection order. Saved decisions and exported pages keep their filename order. On 48 Japanese book photographs from The Metropolitan Museum of Art, the first 12 inspections found 11 of the 18 cuts that missed a marked fold point by more than 2% of image width. Seven misses remained in the other 36. A simple center cut met the tolerance on 45 of these uniformly framed photographs, compared with 30 for Folio Fold. Folio Spread Bench
Choose Save decisions to download fold-decisions.json. Save a partial review at any time and use Load decisions to resume it. Export accepts the saved file and the original scans folder.
folio-fold-batch export ./scans ~/Downloads/fold-decisions.json ./pages
Export writes one numbered folder per included source in review-file order. A kept page gets whole.png. A split gets left.png and right.png. Skipped and pending scans are omitted, so you can export a partial review. The exporter checks each included source’s SHA-256 and writes to a new directory. In pages.json, a whole page has a whole path, and a split has left and right paths plus the cut positions. For a split it gives the right page's horizontal offset for placing both exports back on the original canvas.
Use a flat folder of single-frame JPEG, PNG, WebP or TIFF images, up to 8 bits per channel. Unreadable or unsupported images with these extensions appear in the review's exclusions file. Preview JPEGs fit in 1,100 × 850 pixels. Export uses the original EXIF-oriented raster at full resolution, converts it to 8-bit RGBA, and adds transparent padding along angled cuts. Numbered folders follow the reviewed filename order. The local page bundles Barlow and Instrument Serif with their SIL Open Font License notices in review_assets/.
On one three-CPU Linux job, preparing the 170 native 1,280-pixel-wide benchmark scans took 14.2 seconds and produced a 36.1 MB review folder from 225.9 MB of source images. This timing covers the prepare command, including hashing, inference and JPEG previews.
Show writing lines from PAGE XML
If your scans already have PAGE annotations, add their folder when preparing the review. For scan001.jpg, the reviewer looks for scan001.xml in that folder. Its imageFilename must name the scan and its dimensions must match the EXIF-oriented image. The annotations must use the same framing and orientation as the displayed scan.
folio-fold-batch prepare ./scans ./review-with-lines --page-xml ./scans/page
As you move the cut, the preview reports how many annotated writing baselines have points on both sides. Highlight crossings shows those lines in red. You can switch the highlights off to inspect the underlying strokes. Missing XML, mismatched images and omitted baselines are identified in the preview. The report stores baseline geometry from the XML. The saved decisions contain cut positions and source identifiers.
A baseline can end in the margin beyond the last written character. A low count can also occur on a misplaced cut through a blank part of a page. The benchmark includes full-resolution examples of both a cut through a word and a crossing beside the writing. Annotated examples and PAGE scorer
Start from a library manifest, TIFF or archive book
For an Internet Archive book, select leaves from the JPEG 2000 ZIP and review each proposed cut. The worked example includes the original scan labels and right-to-left reading order. Review a JPEG 2000 book
Use the full-depth importer for unsigned 16-bit grayscale or RGB scans. It opens the local reviewer and exports upright TIFF pages with their original sample values. Review 16-bit TIFFs
For an IIIF library item, select the canvases to download and review. For a multipage TIFF, the importer prepares upright frames and records their source page numbers. Both workflows feed the folder reviewer above. IIIF manifest example · Multipage TIFF example
Turn from the cover to the two split preface pages in the Japanese volume, or click a word on the Dutch scan to see its OCR entry. Read the 1804 Japanese preface · Inspect the Dutch word annotations · Export a book for a IIIF reader
Split a scanned book PDF
The PDF reviewer handles books that mix single pages, covers and two-page spreads. For a two-page example, try the cover and open preface from Hokusai’s eighth sketchbook. Sample PDF · Museum record and credit
hf download foliokestrel/folio-fold config.json install_wheel.py --local-dir folio-fold-install
python folio-fold-install/install_wheel.py --pdf
curl -fL https://huggingface.co/foliokestrel/folio-fold/resolve/main/examples/hokusai-sample.pdf -o hokusai-sample.pdf
folio-fold-pdf prepare hokusai-sample.pdf review
Open review/index.html. Choose Keep whole page on the cover. On the open spread, check the line against the binding and adjust it with Mark two points if needed. Choose Approve cut, select Right page first for this book, then Save decisions. You can resume a partial review with Load decisions.
folio-fold-pdf export hokusai-sample.pdf ~/Downloads/fold-decisions.json reading-copy
Open reading-copy/book.pdf. The cover comes first, followed by the right and left halves of the preface. The exporter uses the order saved in the review. To override it, pass --reading-order left-first or --reading-order right-first. A new review starts with the source PDF’s direction preference, falling back to left page first. Older decisions files without a saved order also use the source preference on export. The default image-only reading copy records the selected direction for viewers that display facing pages. With --keep-ocr, the file preserves the source viewer direction for text extraction. Source pages always retain their original sequence, including when you inspect them in a different order. The output folder also contains pages.json, mapping each reading page to its source page and side.
Both source photographs are public-domain images of Katsushika Hokusai’s Hokusai Sketchbooks, volume 8 (1817), The Metropolitan Museum of Art, Rogers Fund, 1931, accession JIB81.8. Folio Kestrel assembled them as a two-page demonstration PDF. Museum Open Access
By default, export creates an image-only reading copy. For scans with existing invisible OCR, add --keep-ocr to carry verified text objects onto their reviewed pages. Runs crossing the cut or failing a character-position check are counted in pages.json. The sample lets you try a phrase search on each output page. Searchable PDF example, supported text layers and results
Add --keep-bookmarks to map internal chapter destinations to the reviewed output pages. The two-page registers example lets you test both chapter links and searchable text. Run the bookmark example
Keep the original PDF for its forms, signatures and external actions. The exporter renders the displayed crop and rotation at 200 DPI by default, with white padding along angled cuts. Use --dpi 300 for a higher-resolution render. Pages exceeding 40 megapixels at the requested DPI require a lower DPI. Every source page needs a decision. The command checks the source PDF’s SHA-256 and writes to a new directory.
On a two-CPU Linux job, export took 5.3 seconds and peaked at 239 MiB of resident memory for a six-inch-wide test PDF with one cover and 48 photographed spreads. At 200 DPI its 49 source pages became 97 reading pages. The 23.4 MB source produced a 78.7 MB reading copy. This timing excludes preparation and review.
What the comparisons showed
Fresh photographs. After freezing the checkpoint and decoder, we screened 128 Commons photographs from four categories and kept 17 distinct historical volumes. One AI annotator placed three points per book and reviewed magnified crops before running any locator. The table counts photographs on which all three points are within the stated fraction of full image width. These small samples describe this collection.
| Method | Median worst error | Within 1% | Within 2% | Within 5% | Within 2% on 4 slanted folds |
|---|---|---|---|---|---|
| Center | 1.579% | 7/17 | 10/17 | 14/17 | 0/4 |
| Zhnivo | 1.610% | 8/17 | 9/17 | 12/17 | 0/4 |
| Line search | 0.612% | 10/17 | 12/17 | 14/17 | 2/4 |
| Folio Fold | 0.712% | 9/17 | 12/17 | 15/17 | 2/4 |
Only four fresh books had at least 4% of the image width between their first and last fold marks horizontally. The earlier exploratory pilot contained 24 photos. Folio Fold reached 2% on 21/24, line search and Zhnivo on 16/24, and center on 13/24. On the fresh sample, Folio Fold tied line search at 2%. Large errors included the illustrated display case and a glass-covered Gutenberg Bible. Image-level predictions and references · Evaluation and reproduction
Archival scans. For each of 170 held-out cBAD scans, the table counts straight cuts wholly inside the reference gutter interval. The last two conditions use controlled image transformations. On the separate ABP development archive Folio Fold reached the native band on 21/42 scans, compared with 41/42 for both center and Zhnivo. Prefer a validated archive-specific method for unattended work. Benchmark and metric · Zhnivo model
| Method | Native scans | Off-center crop | Artificially sloped crop |
|---|---|---|---|
| Center | 105 | 0 | 0 |
| Zhnivo | 159 | 131 | 0 |
| Line search | 119 | 121 | 117 |
| Folio Fold | 123 | 133 | 131 |
Compare the Freyung checkpoint
On 170 cBAD scans, the Freyung-trained checkpoint placed the cut within the marked band on 125 pages and the default checkpoint did so on 123. Across those scans the Freyung cuts crossed 749 annotated writing lines and the default cuts crossed 687. Inspect the paired examples when choosing a checkpoint for handwritten spreads. Folio Fold Freyung and its writing-line audit
How small can the input be?
A 320-pixel JPEG at quality 55 was sufficient to keep the proposed cut inside the reference gutter on 122 of 170 native archival scans, compared with 123 on the 1,280-pixel benchmark inputs. Four cuts moved outside the gutter and three moved inside. Native photographs were more variable. At the three annotated binding positions, the number within 2% of frame width fell from 21 to 18 on 24 Commons and Cleveland photographs and rose from 30 to 31 on 48 Met photographs. Inspect the full frame before committing a cut. Photograph results and reproduction · Archival preview benchmark
Model and training
A 173,874-parameter convolutional encoder-decoder takes a 160 × 160 grayscale image. It predicts 80 horizontal positions and one outside-book class on each of 40 rows. A robust fit through consistent rows yields the endpoints. Training used 153,600 synthetic book scenes in 3,200 optimization steps, with 2,000 original procedurally typeset textures. The renderer varies framing, perspective-like slopes, ruling, shadows and illumination. We knew the earlier benchmark baseline results when designing the experiment. No real scan or photo entered the training batches. We evaluated the fixed step-3,200 checkpoint without tuning on the fresh labels.
The ONNX graph uses opset 17 with input image of shape [batch, 1, 160, 160] and output row_logits of shape [batch, 40, 81]. The frozen decoder is in infer.py. PyTorch and ONNX endpoints agreed to within 0.000011% of image width across all 24 pilot photos. The same weights are included as model.safetensors for PyTorch. Model and code use Apache-2.0. Example images retain their attributed licenses. Training instructions
- Downloads last month
- 134


