DEV Community

Digital Nerve
Digital Nerve

Posted on Edited on

Receipt OCR with Python and Tesseract: 5 Wrong Totals, 2 Fixes

When we ran our receipt-OCR pipeline against our test set, five receipts produced totals that looked plausible and were wrong. This post walks through the two fixes that mattered most: a fallback line matcher, and matching amounts by position when a receipt splits into columns.

A note on our test data: the receipts in these tests are synthetic. We generate receipts with known correct totals, which lets us say exactly when the pipeline is wrong and why. The walkthrough video below uses the same synthetic set.

The setup

A minimal receipt pipeline in Python looks like this:

import pytesseract
from PIL import Image, ImageOps

img = Image.open("receipt.jpg").convert("L")
img = ImageOps.autocontrast(img)
text = pytesseract.image_to_string(img)
Enter fullscreen mode Exit fullscreen mode

Then you parse the text for candidate amounts and labels. That is where the trouble starts.

Failure 1: the bold "TOTAL" line

The most intuitive approach, find the line that says TOTAL and read its amount, was our single biggest failure source. On several test receipts the bold total line was smudged, merged with neighbouring glyphs, or split mid-word ("T0TAL", "TOTA L", "TOTAL 68. 14"). When the match fails, a naive parser silently falls back to the largest amount on the receipt. On one test receipt that was a quantity-times-price line, not the grand total. The number looked right. It was wrong.

Fix 1: ranked fallback lines. Instead of one pattern, we score candidate lines in priority order: exact "TOTAL" or "GRAND TOTAL" first, then "AMOUNT DUE", "BALANCE DUE", then "CASH". On the failing receipt, the plain "Cash" line gave us the correct 687.14 while the bold total line was unrecoverable. The fallback only works if you check candidates in order and stop at the first parseable amount, and if a silent fallback is logged rather than trusted.

Failure 2: receipts that split into columns

One test receipt rendered as two columns. Tesseract interleaved the left and right columns line by line, so amounts ended up attached to unrelated labels and every text-based match fell apart.

Fix 2: match by position, not by reading order. Use pytesseract.image_to_data to get a bounding box per token, cluster tokens into visual rows by their y-coordinate, then pair each label with the amount on the same row:

import pytesseract

data = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT)
for i, word in enumerate(data["text"]):
    x, y = data["left"][i], data["top"][i]
    # group tokens into rows by y, then pair each label with the amount on that row
Enter fullscreen mode Exit fullscreen mode

Row-matching survives column splits because it never depends on reading order.

What to check before the numbers reach your books

  • The extracted total is at least every line-item amount, and no more than their sum plus tax.
  • A fallback match is logged as a fallback, never presented as a confident read.
  • Low-confidence reads get a human check before they reach the books.

Video walkthrough

We recorded a short walkthrough of these tests with the actual failing receipts and both fixes:

We're Digital Nerve. We build practical automation tools for small businesses, and this pipeline is part of that work. Questions and corrections are welcome in the comments.


Editor's note: this article was drafted with AI assistance from our own test results, and reviewed by the Digital Nerve team before publishing.

Top comments (0)