How to improve OCR accuracy

Updated · Tested with Tesseract 5.5.0, pytesseract 0.3.13, Pillow 13

In short

Resolution and orientation decide most of it. On our test page, Tesseract made no errors at 300 DPI and got half the characters wrong at 60 DPI; enlarging the small image four times brought that under one percent. A sideways page went from 88 percent wrong to none once it was rotated. Set the language, and measure every change.

We read the same page with Tesseract in six conditions and counted the wrong characters each time. The page is the one-page company extract, 383 characters long; the numbers below are the character error rate, the share of characters that came out wrong.

InputTesseract, default settings
300 DPI scan0.0%
100 DPI scan0.5%
60 DPI scan49.9%
Tilted 2.5 degrees0.0%
Sideways88.0%
Blurred, noisy phone photo93.2%

Three things decide almost everything: resolution, orientation, and how clean the image is.

1. Resolution: enlarge small images

Tesseract works best when letters are about 30 pixels tall, which is what a 300 DPI scan gives. At 60 DPI half of the characters were wrong. Enlarging the same image four times before reading it brought that down to under one percent:

Python
import pytesseract
from PIL import Image

small = Image.open("company-scan-60dpi.png")
large = small.resize((small.width * 4, small.height * 4), Image.LANCZOS)
print(pytesseract.image_to_string(large)[:55])
Output
COMPANY EXTRACT

Company name Harbor Lane Logistics Ltd
ImageCharacter error rate
60 DPI, as it is49.9%
60 DPI, enlarged four times0.8%

Enlarging adds no information, but it puts the letters at the size the engine was trained on. If you control the scanner, set it to 300 DPI and skip this step. When you turn PDF pages into images yourself, ask for 300 DPI there too, for example page.get_pixmap(dpi=300) in PyMuPDF.

2. Orientation: turn the page upright

A page scanned sideways is unreadable to the default settings: 88% of characters wrong. Tesseract can detect the rotation; you have to ask.

Python
import pytesseract
from PIL import Image

image = Image.open("company-scan-sideways.png")
osd = pytesseract.image_to_osd(image)
print([line for line in osd.split("\n") if line.startswith(("Orientation in", "Rotate"))])
Output
['Orientation in degrees: 270', 'Rotate: 90']

Rotate by that amount and read again:

Python
angle = int([line for line in osd.split("\n") if line.startswith("Rotate")][0].split(":")[1])
upright = image.rotate(-angle, expand=True)
text = pytesseract.image_to_string(upright)
ImageCharacter error rate
Sideways, default settings88.0%
--psm 1, which detects orientation while reading0.3%
Rotated first, then read0.0%

Detection needs a fair amount of text on the page. On a page with a few words it can fail or guess wrong, so check the result on short documents.

A small tilt is less of a problem than you might expect: at 2.5 degrees Tesseract made no errors on this page. Past a few degrees, straighten the image first; OCRmyPDF does it with --deskew.

3. Noise: sometimes the answer is a better picture

The phone photo is small, blurred, and speckled. Default Tesseract got 93% of it wrong, and nothing we tried rescued it:

What we tried on the photoCharacter error rate
Nothing93.2%
Greyscale, enlarged three times67.9%
Enlarged, median filter, automatic contrastFar worse: pages of invented characters

That last row is the lesson. Filters that help one image wreck another, and a filter that turns speckles into blobs gives the engine more shapes to misread. Measure before and after on your own files, as how to measure OCR accuracy shows, and keep a step only if the number goes down.

For photos, the cheapest fix is at the source: flat page, good light, the page filling the frame, no flash glare.

4. Tell Tesseract what it is looking at

  • Language. The default is English. A page in another language read with the English model gives nonsense, not an error. Pass lang="heb+eng" or whichever languages the page uses.
  • Page layout. --psm sets how the page is segmented. The default (3) suits full pages. Use 6 for a single block of text and 7 for one line, such as a cropped field. The wrong mode for a page can produce garbage.
  • Characters. For a field that holds only digits, -c tessedit_char_whitelist=0123456789 stops a 0 being read as an O.

5. Check the confidence

Tesseract reports a confidence for every word. Use it to route doubtful results to a person instead of trusting all of them; the code is in how to OCR a scanned PDF in Python.

OCR and 100sands

100sands does not do OCR, so none of the six files above can be sent to it: it reads PDFs and HTML files that contain text, refuses a scanned PDF with status 422 and the code NO_TEXT_IN_FILE, and refuses an image file with 415. For scans and photos, the steps in this guide, with Tesseract or another OCR engine, are the way to better results.

Questions

What DPI is best for OCR?

300 DPI. On our test page Tesseract made no errors at 300 DPI, a few at 100 DPI, and got about half the characters wrong at 60 DPI.

How do I improve Tesseract accuracy on a low-resolution image?

Enlarge it before reading. Resizing a 60 DPI image four times with Lanczos resampling cut the character error rate from 49.9 percent to 0.8 percent in our test.

Why is OCR returning garbage?

The usual causes are a page that is sideways or upside down, the wrong language model, or an image that is too small. Check orientation with Tesseract's --psm 0, set the language, and render at 300 DPI.

Does image preprocessing always help OCR?

No. On a noisy phone photo, a median filter and automatic contrast made Tesseract's result far worse. Measure the error rate before and after each step and keep only what lowers it.

Try it on your own documents

A free plan every month, no credit card. See the PDF to text API.

Start free