Enhance a scanned PDF
99.9%
of our aged scan paper came out white
Yellow paper, gray scans, shadows, show-through. One free command.
One command, then OCR for search
python3 -m venv clean-venv
. clean-venv/bin/activate
pip install numpy pillow
python3 clean_scan.py scan.pdf scan-clean.pdf
ocrmypdf scan-clean.pdf scan-clean-ocr.pdf
It divides each page by a blurred copy of itself. Slow changes in the paper go white, and the text stays. clean_scan.py, MIT license.
It writes gray pages. To keep colored ink and stamps, add --color. It
needs poppler, and the last line needs OCRmyPDF:
brew install ocrmypdf poppler on a Mac.
python3 clean_scan.py scan.pdf scan-color.pdf --color
White paper on every kind we tried
| Kind | As scanned | clean_scan.py |
|---|---|---|
| Yellowed paper | 23.8% | 100.0% |
| Gray, dark scan | 0.0% | 100.0% |
| Shadow at one edge | 50.8% | 100.0% |
| 4 new kinds, script frozen | 0.6% | 100.0% |
- Show-through: 34.2 gray levels dark as scanned, 0.6 after.
- Faded ink: gray 160 as scanned, 67 after (0 is black).
Other ways, and what they broke
| Way | Paper white | Tesseract words |
|---|---|---|
| As scanned | 2.3% | 721, 472 false |
| OCRmyPDF --clean-final | 2.2% | 603, 501 false |
| magick -level 20%,80% | 85.9% | 904, 136 false |
| magick -lat 50x50-5% | 98.8% | 896, 813 false |
| magick +dither -colors 8 | 5.0% | 639, 621 false |
| clean_scan.py | 99.9% | 943, 22 false |
- -lat, a local threshold: white paper, but the back page came through as black, mirrored words.
- -level: one setting for the whole page, so part of the shadow stays. Faded ink got lighter, gray 182.
- --clean-final and -colors 8: both left the yellow paper, and Tesseract found fewer words.
OCR gains only on bad pages
| Reader | As scanned | clean_scan.py |
|---|---|---|
| Apple Vision | 961, 119 false | 962, 5 false |
| Our Actor's code, build 0.1.102 | 842, 294 false | 947, 18 false |
| OCRmyPDF, the commands above | 687, 421 false | 949, 14 false |
On 10 real book pages, the words barely changed: OCRmyPDF found 1,932 and 1,931 of 1,955. Clean those for the eye, not for search.
Where it can hurt
- Light notes: on our faded page it erased a light gray note, like pencil. Apple Vision read it before, not after.
- Light lines: our form lost its gray header row, and its table lines got faint.
- Small print: after it, Apple Vision misread a line or two of our 8 pt print. Tesseract did not.
- Specks and tilt: it does not remove dust or hairs, and it does not straighten pages.
- Black and white scans: a 1-bit scan has white paper already and would get bigger.
Check the first pages before you run a whole book.
Smaller files on our scans
| File | As scanned | clean_scan.py |
|---|---|---|
| 6 aged pages | 5.1 MB | 2.1 MB |
| 10 book pages | 6.5 MB | 4.7 MB |
Our scans were color JPEG. It writes gray JPEG pages at quality 75. With --color, the 6 pages came to 2.3 MB.
Then turn it into text
Our Apify Actor turns scanned PDFs into Markdown and text. Its code is in the readers table above.
The free plan takes 20 pages a run, 50 a month. An OCR page costs $0.006.
How I tested it
- 6 made-up pages (a letter, a memo, an invoice, two columns, 8 pt small print, a form), each in 8 aged kinds as simulated scans: 48 pictures.
- 10 real pages of 2 public domain books, 5 on tan paper.
- 24 new page scans in 4 new kinds (blue, pink, darker paper, a deeper shadow), made after the script was frozen.
- Tesseract 5.5.4, Apple Vision on macOS 26.6, OCRmyPDF 17.13.0, ImageMagick 7.1.2, unpaper 7.0.0. Our Actor's code at commit 528fe416 (build 0.1.102). No Apify run.
What I did not test
ScanTailor, Acrobat's Enhance Scans, online tools, phone scanner apps, color documents with stamps, handwriting, 1-bit scans, and languages other than English.
Questions people ask
How do I enhance a scanned PDF for free?
Run clean_scan.py on it. It turns yellow, gray or shadowed paper white and makes the text darker. Then run OCRmyPDF if you want search. Both are free.
How do I remove the yellow background from a scan?
Divide the page by a blurred copy of itself, then stretch the levels. clean_scan.py does that. A plain levels change only partly fixes a shadow, because it uses one setting for the whole page.
Does cleaning a scan improve OCR?
On bad scans, yes: Tesseract found 943 of 966 words on our fully aged pages, up from 721. On 10 real book pages it barely changed the words.