OCR Test Files: A Fixture Set for Clean, Blurry, Tilted, and Multilingual Text
OCR pipelines have a nasty property: they never hard-fail. A broken upload returns a 500; a broken OCR engine returns confident garbage with a straight face. The only way to catch that class of bug before your users do is a fixture set that spans the quality spectrum your production documents actually occupy — clean scans, phone photos at bad angles, faxes that look like weather noise, and text in scripts your engine may have never met. Here is a practical matrix, the commands to generate every variant, and how to score the results in CI.
The Fixture Matrix
One perfect image tests nothing. The minimum viable OCR fixture set looks like this:
| Fixture | What it looks like | What it catches |
|---|---|---|
| Clean baseline | 300 DPI, black on white, no skew | Your accuracy ceiling |
| Low resolution | Downscaled to ~72 DPI | Minimum glyph size handling, upscaling fallbacks |
| Blur | Gaussian radius 1-3 | Preprocessing thresholds, binarization limits |
| Skew | Rotated -5 to +5 degrees | Deskew stage, or the lack of one |
| Noise | Salt-and-pepper speckle, gray background | Otsu/Sauvola binarization robustness |
| Multilingual | CJK, Arabic, mixed scripts | Language packs, font fallback, RTL ordering |
| Image-only PDF | Raster page wrapped in a PDF | Rasterization path, text-layer detection |
Store the ground-truth text next to each fixture as fixture.txt so scoring stays mechanical — a plain-text sibling file beats any spreadsheet.
Every row in that table has broken a real pipeline somewhere. The low-resolution row catches code that assumes scanner-grade input; the noise row catches binarization tuned only on clean scans; the multilingual row catches English-only regular expressions doing post-processing. Seven base fixtures plus their variants fit in a few hundred kilobytes and run in seconds — there is no excuse for a suite with only the first row.
Generating Every Variant
Start from a base image you fully control, then derive the degenerate cases with one command each:
# make_base.py - render text to PNG with Pillow from PIL import Image, ImageDraw, ImageFont img = Image.new('RGB', (1240, 1754), 'white') # A4 at 150 DPI d = ImageDraw.Draw(img) font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 28) d.text((120, 200), 'Invoice 2026-0042\nTotal due: USD 1,249.00\nDue date: 2026-12-01', fill='black', font=font) img.save('clean.png')
# variants.py - derive the whole matrix from PIL import Image, ImageFilter import random img = Image.open('clean.png') img.resize((img.width // 4, img.height // 4)).save('lowres.png') img.filter(ImageFilter.GaussianBlur(2)).save('blurry.png') img.rotate(3, expand=True, fillcolor='white').save('skewed.png') # CCW 3 deg # salt-and-pepper noise px = img.load() for _ in range(20000): x, y = random.randint(0, img.width - 1), random.randint(0, img.height - 1) px[x, y] = random.choice([(0, 0, 0), (255, 255, 255)]) img.save('noisy.png')
Prefer one-liners? ImageMagick does the same job: magick clean.png -blur 0x2 blurry.png, magick clean.png -rotate 3 skewed.png (ImageMagick 6 users: swap magick for convert).
Two details matter more than they should. Resolution: 300 DPI is the standard floor for OCR. At 72 DPI a 12pt glyph is only about 12 pixels tall, and engines start dropping ascenders and confusing rn for m. Skew: accuracy falls off noticeably once rotation passes a couple of degrees, so test at 0, 3, and 8 degrees rather than one token rotation.
If you would rather start from ready-made raster fixtures, take a sample PNG or a sample JPG and mutate from there — the point of the matrix is that every variant is one command away from the baseline.
Multilingual and Script Coverage
Tesseract is still the default open-source baseline, and it loads one traineddata file per language. The naming is a classic trap: Debian packages use hyphens while the -l flag uses underscores:
sudo apt install tesseract-ocr tesseract-ocr-chi-tra tesseract-ocr-jpn tesseract multilingual.png stdout -l eng+chi_tra+jpn
CJK text exposes a different failure class than Latin. Glyph density is higher, so the same 300 DPI that works fine for 12pt English can be marginal for 9pt Chinese — bump small CJK text to 400 DPI and up. Arabic and Hebrew add right-to-left ordering bugs that an English-only suite will never surface. And test mixed-script pages: English invoice numbers inside Japanese prose is the real world, not an edge case.
If your product page claims "any language," the fixture set is where that claim is either true or marketing.
Handwriting is the honest boundary. Tesseract is not a handwriting engine, and neither are most commercial SDKs — but include one handwritten fixture anyway, score it, and record the number. When someone asks "can we handle handwriting?", you will have data instead of a shrug.
PDFs: Where OCR Pipelines Go to Die
Most production OCR input is PDF, and PDFs come in two species your tests must distinguish: text-layer PDFs (extraction should be direct and near-perfect) and image-only PDFs (must be rasterized before OCR). Pipelines that treat them identically are broken for one of the two.
# rasterize an image-only PDF at 300 DPI pdftoppm -r 300 -png input.pdf page tesseract page-1.png stdout -l eng --psm 3 # guarantee an image-only PDF from your clean fixture pip install img2pdf img2pdf clean.png -o image-only.pdf
For a known-content sample PDF to mutate, generate your own rather than reaching for random downloads — provenance beats variety. And do not skip TIFF: it is the historical scanner format and still common in fleet hardware. Multi-page TIFF is where many pipelines quietly process page 1 and report success, so a sample TIFF with several pages belongs in the matrix.
Scoring: CER, Not Vibes
"Looks right" is not a metric. Character Error Rate is:
CER = edit_distance(ocr_output, ground_truth) / len(ground_truth)
# score.py import sys from rapidfuzz.distance import Levenshtein ocr = open(sys.argv[1]).read() gt = open(sys.argv[2]).read() cer = Levenshtein.distance(ocr, gt) / len(gt) print(f'CER {cer:.1%}') sys.exit(0 if cer <= float(sys.argv[3]) else 1)
Assert per fixture with explicit budgets: clean under 2%, blurry under 10% — whatever thresholds reflect your product. A regression from 1.5% to 9% on the clean baseline becomes a build failure you can act on, instead of a support ticket three weeks later.
CI Recipe
- name: OCR fixture matrix run: | python make_base.py && python variants.py for f in clean lowres blurry skewed noisy; do tesseract $f.png stdout -l eng > $f.out python score.py $f.out $f.txt 0.10 done
Ship the matrix, not one pretty scan. Your users' documents are already blurry, tilted, and multilingual — your test set should have gotten there first.
Related Posts
How to Test File Upload Features: A Developer's Guide
Learn the best practices for testing file upload functionality in web applications using sample files.
Read moreTestingSample PDF Files for Testing: Complete Guide
Everything you need to know about using sample PDF files for development and testing.
Read moreTestingTesting File Size Limits: KB, MB, GB Upload Scenarios
Learn how to test file upload size limits with sample files ranging from 1KB to 100MB.
Read more