Turn phone photos of paper into a clean, straightened PDF. For the pages you already photographed and can't reshoot.
Point it at a folder of photos. It finds the sheet of paper in each one, flattens it, and writes a single A4 PDF.
If you can go back and rescan the pages, use the scanner built into your phone instead. This exists for the photos you already have: the ones taken at an angle, on a kitchen table, in a hurry, that you now need as documents.
Everything happens on your machine. It makes no network calls.
pip3 install opencv-python numpy pillow pillow-heif
git clone https://github.com/jgalea/document-scanner.git
cd document-scanner
chmod +x scan.pyThat's macOS and Linux. On Windows, install Python (winget install Python.Python.3.12), then in a new PowerShell window run py -m pip install opencv-python numpy pillow pillow-heif, download or clone the repo, and run the script as py scan.py wherever the examples below say ./scan.py. The Windows steps haven't been tested on a real Windows machine yet.
# Every JPG in the current folder, one page per photo, out to ./scanned.pdf
./scan.py
# A folder of open-book photos, split each one at the binding
./scan.py ~/Downloads/book/ -o ~/Downloads/book.pdf --split-spreads
# Mixed: photos 0 and 6 are single-page covers, the rest are spreads
./scan.py ~/Downloads/book/ -o ~/Downloads/book.pdf --split-spreads --cover 0 --cover 6- Loads it with EXIF rotation applied.
- Finds the paper as the largest four-corner contour, trying edge detection first and brightness thresholding second.
- Applies a perspective transform to flatten it, which removes the desk, the shadow along the paper's edge and the skew in one step.
- With
--split-spreads, finds the darkest vertical band near the centre and cuts there. That band is the binding. - Lifts the contrast mildly, 50 to 0 and 225 to 255. Pencil stays readable, and it avoids the bleached look that scanner apps produce.
- Pads to A4 portrait on white.
- Combines the pages into one PDF.
A single-page cover shot without a blank facing page can fool the contour detector, which sometimes picks a larger quadrilateral that includes the table. Mark those photos with --cover INDEX.
Thick books won't fully flatten, because a perspective transform assumes the page is flat and a heavily curved spread isn't.
It works best on photos taken from directly above, with the paper on a dark background, in reasonable light.
MIT. See LICENSE.
Author: Jean Galea.