Free zonal OCR in Python, tested
450 of 454
typed values exact on scans
One script reads every box
Zonal OCR reads each form box on its own.
pip install opencv-python-headless numpy pypdf
brew install tesseract poppler
python zonal.py blank.pdf 1 scan.jpg > fields.json
The boxes come from the blank PDF's own form fields. No fillable
PDF? Pass a zones.json as the 4th argument, each box in
points from the top left.
What does the script do, step by step?
- It draws the blank page at 200 dpi.
- It finds the same points on the scan (SIFT) and warps the scan onto the blank page.
- It erases every pixel that is ink on the blank form.
- It skips a box with fewer than 20 dark pixels left.
- It reads the other boxes in one Tesseract run, one line each, and multiline boxes as a block.
One SS-4 page took 4 to 5 s on our Mac.
Erase the form, then read
| Way | Scans | Photos |
|---|---|---|
| Cut the boxes, no line-up | 123 | 0 |
| Line up with ORB, cut | 432 | 405 |
| Line up with SIFT, cut | 428 | 410 |
| ORB, form erased | 451 | 433 |
SIFT, form erased (zonal.py) |
450 | 434 |
| Tesseract on the whole page | 416 | 313 |
| Apple Vision on the whole page | 432 | 442 |
The line-up matters most. Our scans turned under 1 degree, yet they lost 331 of 454 values without it.
ORB and SIFT were within 1 value once the form was erased. ORB (--align orb) ran a little faster, but it left junk in 3 empty photo boxes.
Empty boxes gave junk text
141 of 146 empty scan boxes gave junk, lined up and cut, not erased
0 of 146 with the form erased first
Even a pure white box is a trap. Tesseract printed
re, Be or Bn for 3 of 5 white
box sizes.
Can I erase more of the form to be safe?
No. Erase too much and you erase the fill. Growing the erase by 2 more pixels cost 14 of 153 photo values on our tuning forms.
Whole-page OCR, then boxes
The other way: read the whole lined-up page once, then give each word to the box that holds its centre.
Apple Vision did best on photos. But it read the comma as a period in 13 values: 7 on photos, 6 on scans.
So 6,454.29 became 6.454.29. Tesseract did worse on photos.
Need all the text? Our Actor
Our Apify Actor reads the whole form and returns Markdown, with no boxes. Its text held 424 of 454 values on scans (build 0.1.97) and 364 on photos (build 0.1.96), but not which box each came from.
A scanned page costs $0.006, and each run has a $0.00005 start fee.
curl -s -X POST "https://api.apify.com/v2/acts/brasspage~pdf-ocr-to-markdown/run-sync-get-dataset-items" \
-H "Authorization: Bearer $APIFY_TOKEN" \
-H "Content-Type: application/json" \
-d '{"documentUrls": ["https://example.com/form.pdf"]}' \
| jq -r '.[].markdown' > form.md
How I tested it
- Forms: 6 IRS pages from irs.gov (W-9, SS-4, 1040 page 2, 8822, 8879, 2848 page 2). They are public domain.
- Fills: 4 a page, made-up values typed in Helvetica in about 3 of 4 text fields. Comb, multiline and very short fields were left out.
- Each fill as a gray scan at 200 dpi and a simulated 12 MP phone photo. Renders, not real scans or photos.
- Right means the box text equals the value exactly, case kept, spaces collapsed.
- The script was tuned on 3 other IRS forms (W-4, 8821, 4506-T) and frozen before the test run.
- Tesseract 5.5.4, OpenCV 5.1.0, pypdf 7.1.1, Poppler 26.03.0, macOS 26.6.
- The Actor's code (build 0.1.96) failed on 2 of the 24 scans, both 1040 page 2, with an error in its table cell crop. Build 0.1.97 fixes it, live since Nov 19: 0 errors on the 24 scans, the same text on the other 22, and 424 values found. I did not run the photos again. Build 0.1.96 had no error on them.
- Not tested: handwriting, comb fields, real scanner output, paid zonal OCR apps.
Questions people ask
What is zonal OCR?
OCR of set boxes on a page, like the name or the amount on a form. Each box gets its own text, so you know which field the text came from.
How do I do zonal OCR in Python for free?
Line the scan up with the blank form (OpenCV), erase the blank form's own print, then read each box with Tesseract. Our zonal.py does all three. On 24 scanned IRS forms it read 450 of 454 typed values exactly.
Why does Tesseract read junk in an empty box?
The box still holds the form's own lines, labels and shading, and Tesseract reads them as letters. Even a pure white box came back as re, Be or Bn. Erase the blank form first and skip boxes with no ink.
Related guides
- Business card scanner to Excel: 5 free ways on 24 cards
- Checkbox detection in Python: 3 free tools on 768 boxes
- Signature detection in Python: 3 free tools on IRS forms
- Stamp detection in Python: 5 free tools on IRS forms
- OpenCV OCR in Python: text detection and recognition, tested
- Invoice OCR in Python: a pytesseract script, tested