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.
| Input | Tesseract, default settings |
|---|---|
| 300 DPI scan | 0.0% |
| 100 DPI scan | 0.5% |
| 60 DPI scan | 49.9% |
| Tilted 2.5 degrees | 0.0% |
| Sideways | 88.0% |
| Blurred, noisy phone photo | 93.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:
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])COMPANY EXTRACT
Company name Harbor Lane Logistics Ltd| Image | Character error rate |
|---|---|
| 60 DPI, as it is | 49.9% |
| 60 DPI, enlarged four times | 0.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.
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"))])['Orientation in degrees: 270', 'Rotate: 90']Rotate by that amount and read again:
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)| Image | Character error rate |
|---|---|
| Sideways, default settings | 88.0% |
--psm 1, which detects orientation while reading | 0.3% |
| Rotated first, then read | 0.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 photo | Character error rate |
|---|---|
| Nothing | 93.2% |
| Greyscale, enlarged three times | 67.9% |
| Enlarged, median filter, automatic contrast | Far 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.
--psmsets 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=0123456789stops 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.