| name | photo-to-scanned-pdf |
| description | Turn phone photos of paper documents (contracts, stamped certificates, receipts, forms, handwritten notes) into a clean scanner-quality PDF: perspective rectification + noteshrink background-whitening + A4 PDF assembly, with colored-paper white-balance handling and a mandatory whole-document visual check. Use this skill whenever the user has photos of paper documents and wants them as a PDF / ๆซๆไปถ / scan โ trigger on "ๆ็
ง็ๅๆๆซๆไปถ", "photos to scanned PDF", "make this look scanned", "ๆๆบๆ็ๆๆกฃ่ฝฌ PDF", "็็ซ ๆไปถๆซๆ", replacing pages inside an existing scanned PDF, or any CamScanner-like request. Do NOT hand-roll levels/contrast enhancement for scan-look output โ that path was tried and rejected twice; this skill's pipeline is the proven one. |
Photo โ Scanned PDF
Phone photos of documents โ scanner-look PDF. The pipeline that works, and the
failure modes that ship wrong PDFs if skipped.
photos โโโบ rectify (photo_to_scan.py --raw)
โโโบ ORDER BY CONTENT, detect colored paper โ agent eyes, not filenames
โโโบ enhance: noteshrink (white batch with -g โ colored pages separately,
after white-balance pre-pass)
โโโบ assemble_pdf.py โ A4 PDF
โโโบ make_contact_sheet.py โ READ IT, verify EVERY page โ mandatory
Division of labor: scripts carry execution; you (the agent) carry the two
judgment steps โ content-based page ordering, and whole-document verification.
Neither can be automated away: filenames lie about order, and per-page spot
checks miss wrong-slot bugs.
Step 0 โ Dependencies
which pdftoppm || brew install poppler
uvx noteshrink --help | head -3
Scripts are uv run single-file scripts (PEP 723); OpenCV/PIL/img2pdf resolve
automatically on first run.
Step 1 โ Rectify
uv run <skill>/scripts/photo_to_scan.py --raw --out-dir work --prefix page \
photo1.jpg photo2.jpg ...
Expected: one page_NN.jpg per photo, each tagged [quad]. A
[FULLFRAME-fallback] tag means the paper outline wasn't found (busy background,
page cut off) โ view that photo and decide: retake, or accept the uncropped frame.
The script handles EXIF rotation internally (cv2.imread ignores EXIF; phone
photos come rotated โ this silently produces sideways pages if you rectify with
raw OpenCV).
Step 2 โ Order by content, detect colored paper (agent judgment)
Read every rectified image (batch of ~6 per message) and record two things:
- Its identity โ date, title, page number, whatever distinguishes pages.
Batch-exported photos (WeChat, AirDrop) get timestamps of the export
moment, often all within one second โ filename order is meaningless. Real
case: 17 photos turned out to be in exact reverse document order; only
content reading caught it.
- Its paper color โ white, or colored (blue/yellow/pink stock)? Colored
pages take a different path in Step 3. If unsure, sample programmatically:
mean RGB of a blank region;
B > R + 25 โ blue-ish paper.
Build the final page order as an explicit list before proceeding. If pages are
supposed to match an external register (an invoice list, a session table),
cross-check identity against it now โ missing/duplicate pages found here cost
seconds; found after delivery they cost a redo.
Step 3 โ Enhance (noteshrink, split by paper color)
White-paper pages โ one batch, global palette:
uvx noteshrink -w -g -K -q -b ns -c "true" page_03.jpg page_01.jpg page_07.jpg ...
-w white background, -g one global palette (uniform ink/stamp color across
pages), -K keep given order, -c "true" skips its internal PDF step (we
assemble ourselves).
- Pass filenames explicitly. zsh does not word-split
$VAR โ a file list in
a variable arrives as one giant "filename", noteshrink exits without output,
and -q keeps it silent. Verify outputs exist (ls ns0*.png) rather than
trusting stdout.
Colored-paper pages โ separate, with white-balance pre-pass:
uv run <skill>/scripts/photo_to_scan.py --out-dir work --prefix wb colored_photo.jpg
uvx noteshrink -w -g -K -q -b nc -c "true" work/wb_01.jpg
Two distinct failure modes force this split (both shipped as bugs before the
rule existed):
- Colored pages inside the
-g batch poison the whole document โ the paper
color enters the global palette and white pages come out with tinted
shadows/artifacts.
- noteshrink alone on colored paper whitens the background but not the
foreground cast โ black ink photographed on blue stock reads blue-purple, a
red stamp reads maroon. The default (non-
--raw) mode of photo_to_scan.py
divides out the paper color first, so ink returns to black and stamps to red.
Step 4 โ Assemble
uv run <skill>/scripts/assemble_pdf.py --out scanned.pdf \
ns0000.png ns0001.png nc0000.png ns0002.png ...
Expected: OK scanned.pdf (N pages, ~0.05 MB/page). Edge crop (default
24px top / 12px sides at 200 dpi) removes the sliver of desk surface that
rectification drags in along page borders; document margins dwarf it.
Step 5 โ Verify the WHOLE document (mandatory, not optional)
uv run <skill>/scripts/make_contact_sheet.py scanned.pdf --out contact.png
Read contact.png and check every page: identity sequence complete and
correct (each date/title where it should be, no duplicates, none missing), no
off-color page, stamps/signatures present. Then spot-read 1โ2 pages at full
resolution for text sharpness.
Why whole-document, every time: two shipped-bug stories from the session this
skill was distilled from โ
- A page-replacement task wrote the new page into the wrong slot (an
off-by-one in a copy command), silently overwriting a neighboring page. The
per-page check of the replaced slots passed; the clobbered neighbor was only
caught by the user.
- A palette-poisoning bug (Step 3 #1) tinted pages that were not being
edited. Checking only the edited pages missed it.
The cost asymmetry is absolute: contact sheet = one Read; a wrong page in a
delivered PDF = redo + lost trust. "I verified the pages I changed" is not
verification.
Replacing pages in an existing scanned PDF
Keep the per-page enhanced PNGs (ns*/nc*) as the working set. To replace page
k: process the new photo through Steps 1โ3, overwrite that page's PNG, re-run
Steps 4โ5. When copying into numbered slots, mind the mapping โ slot numbers
shift when photo order was reversed; derive the slot from the page's content
identity, never from its position in the photo batch. Then the Step 5 full
check is what actually protects you.
Troubleshooting
| Symptom | Cause / fix |
|---|
| Output "doesn't look scanned" โ gray haze, soft text | You hand-rolled levels/curves/divide enhancement. Don't โ two attempts were rejected by a real user before switching to noteshrink (background sampling + palette quantization is what produces the flat-white scan look). |
| White pages have tinted shadows | A colored-paper page was inside the -g batch. Re-run whites-only batch (Step 3). |
| Ink looks blue/purple, stamp looks maroon on a colored page | noteshrink got the colored page raw. Insert the white-balance pre-pass (photo_to_scan.py without --raw). |
| noteshrink produced no output, no error | File list passed via an unquoted shell variable under zsh (no word splitting), or paths with spaces. Pass explicit filenames; check ls ns0*.png. |
| Page sideways / upside down | EXIF ignored somewhere upstream, or the quad landed landscape. photo_to_scan.py corrects EXIF + rotates to portrait; upside-down pages it cannot know โ catch at Step 2 and rotate the source photo. |
[FULLFRAME-fallback] on a photo | Paper outline not detected (low contrast vs table, page cut off). Retake against a dark background, or accept full frame + rely on edge crop. |
| Thin dark strip along page edge in the PDF | Desk surface dragged in by rectification. Raise --crop-top/--crop-side in assemble_pdf.py. |
| Pages in wrong order in the PDF | Filename-order assumption. Order comes from Step 2 content reading, passed explicitly to noteshrink (-K) and assemble_pdf.py. |