Erase handwriting from a scan or PDF
0.0%
of blue pen, red pen and pencil left
One command, print kept
magick filled.jpg -channel RGB -separate -evaluate-sequence max -threshold 35% blank.png
ImageMagick keeps each pixel's brightest color channel, then makes it black or white. Colored ink is bright in one channel, pencil is lighter than print, and black print is dark in all three.
It kept 100% of the print pixels on all 60 pages, the ones under the ink too, in 1.3 to 1.4 s a page.
Use 35%, not 50%
| Ink | At 50% | At 35% |
|---|---|---|
| Blue pen | 0.5% | 0.0% |
| Red pen | 0.0% | 0.0% |
| Pencil | 2.2% | 0.0% |
| Simulated photo, blue pen | 83.6% | 2.9% |
| Black pen | 94.1% | 79.7% |
In a shadow, blue ink falls under 50% and turns black. At 25%, the print starts to go: 99.1% kept on the blank pages.
Tesseract read 90 hand words on the blue pages as they were, and 0 after the command.
Black pen stays
Black pen is as dark as the print, so no way we tried removed it and kept the print. At 35%, 79.7% of it stayed.
ImageMagick's color replace with a black pick took the print with it: 1.5% kept. If you can get a blank copy, scan that instead.
Gray shading turns white
A threshold makes every pixel black or white, so the gray boxes on our sheets went: 0.0% kept. Our script removes pixels with color and a 2 px ring around them, and kept 100% of the shading away from the ink.
pip install numpy pillow
python3 erase_ink.py shaded.jpg shaded-blank.png
Without --light 130 it cannot see pencil (with it, 5.3% left, and the shading goes too). It cannot see black pen. On our photos it left 52.7% of the blue ink. erase_ink.py, MIT license.
Pen marks in a PDF lift off
Our test PDF holds its pen strokes as Ink annotations, on top of the page. Our script deletes those and keeps the rest.
pip install pypdf
python3 remove_ink.py marked.pdf clean.pdf
- Pen marks
- 235 of 235
- removed
- Other pixels
- 0.0%
- changed
- Text layer
- 541
- printed words, as before
remove_ink.py, MIT license.
A flattened PDF is a picture
We flattened a copy with pdftocairo, which prints the marks into the page. remove_ink.py then found 0. Erase the pages as pictures:
pdftoppm -r 300 -png scan.pdf page
for f in page-*.png; do magick "$f" -channel RGB -separate -evaluate-sequence max -threshold 35% "clean-$f"; done
magick clean-page-*.png clean-scan.pdf
ocrmypdf -q clean-scan.pdf clean-searchable.pdf
1.7% of the marks stayed, and the gray shading went. pdftocairo had dropped the text layer, and the last line adds a new one: pdftotext found 566 words in all.
Two other ways did worse
| Way | Ink left | Catch |
|---|---|---|
| -opaque, fuzz 15% | 12.1% | stroke edges stay |
| -opaque, fuzz 25% | 2.3% | 95.7% of print kept |
| Keep only OCR word boxes | 65.6% | Tesseract reads the hand |
-opaque paints the ink color white and took 6.8 s a page. On a photo we checked, the hand words came back as white writing on gray paper.
Need the text? Erase first
Our Apify Actor turns scans and PDFs into Markdown. On 10 filled blue pages it put 117 hand words in its text. After the command: 0.
On the PDF, it read 67 hand words from the pen marks. After remove_ink.py: 0, and 537 printed words, like the blank PDF.
The free plan takes 20 pages a run, 50 a month. An OCR page costs $0.006. Pages it reads from a text layer are free: 5 of 10 on our test PDF.
How I tested it
- 10 made-up worksheets of our own text: a quiz, a permission slip, a chores chart and more. 592 printed words.
- Each filled in blue pen, red pen, black pen and pencil, plus 10 simulated photos of the blue copy, with a shadow. 172 hand words and 63 marks a set.
- The hand is a font (Shadows Into Light, Bradley Hand), each word turned and moved a little. The scans and photos are simulated: blur, tilt, noise, JPEG.
- Ink left counts visible ink pixels away from the print. Print kept counts print pixels still dark.
- The PDF: the 10 blank sheets with an OCRmyPDF text layer, plus 235 Ink annotations written with pypdf.
- ImageMagick 7.1.2, Tesseract 5.5.4, OCRmyPDF 17.13.0, Poppler 26.03.0, on our Mac. Our Actor's code at commit 5c8f91b0 (build 0.1.100). No Apify run.
What I did not test
Real handwriting, real scans and phone photos, gel and felt pens, highlighter, light gray or color print, pictures on the page, green or purple ink, Acrobat, Preview, phone apps, online AI erasers and inpainting models.
Questions people ask
How do I erase handwriting from a scanned page?
Keep each pixel's brightest color channel, then make it black or white at 35%. On our scans that left 0.0% of blue pen, red pen and pencil, and kept 100% of the print.
How do I erase handwriting on a PDF?
If the marks are pen annotations, remove_ink.py deletes them: 235 of 235, and 0.0% of other pixels changed. If the PDF is a scan or flattened, turn the pages into pictures and erase those.
Can I remove black pen from a printed page?
Not with the ways we tested. Black pen is as dark as the print. At 35%, 79.7% of it stayed, and a color replace took the print with it.
How do I erase handwriting from a photo?
Use 35%, not 50%. On our simulated photos with a shadow, 50% left 83.6% of the blue ink and 35% left 2.9%.