Dev.to WebDev 🛠 Dev 👁 0 📖 4 min read

Why phone scans of text come out broken, and what a scanner filter should do about it

You hold the phone over a letter, tap the shutter, and the app hands you a crisp white page. Then you zoom in and the small print looks chewed: letters with holes in them, thin lines that fade out halfway, a grey footer

You hold the phone over a letter, tap the shutter, and the app hands you a crisp white page. Then you zoom in and the small print looks chewed: letters with holes in them, thin lines that fade out halfway, a grey footer that is simply gone.

We hit this while building the document scanner in our own app, and the cause was not where we first looked. This post walks through what actually breaks the letters, how to measure it, and the fix that worked for us.

The photo was fine. The filter broke it.

The first instinct is to blame the camera or the resolution. So we saved the raw photo before any filtering and compared.

Two shots of the same printed page, same light:

  • A sharp shot. After the "Auto" filter, the letters were clean.
  • A slightly soft shot, the normal kind you get holding a phone by hand. In the raw photo every word was still readable. After the filter, the small print was broken into fragments.

Same result at full size and at a reduced size, so it was not resolution, and not the save path either. The filter was turning readable soft text into broken text.

Why a "make the paper white" filter eats soft letters

Most scanner filters do roughly three things:

  1. Estimate the paper colour and push it to pure white (a white point).
  2. Apply a steep tone curve so ink gets darker and paper gets whiter.
  3. Sharpen a little.

On a sharp photo, a letter stroke is a dark core with a very thin grey edge. The curve makes the core black, the edge falls either way, and the letter looks crisp.

On a soft photo, the same stroke is a dark core with a wide grey fringe. Now the steep curve does something nasty: everything lighter than the cut-off goes to white. The fringe vanishes, and because the core of a thin stroke is itself not very dark when it is blurred, parts of the core vanish too. You get letters with holes along their edges.

It gets worse with a page-level strength setting. If the filter decides how hard to darken ink by looking at the boldest things on the page (a heading, a table border), then on a page with a strong title it concludes "ink is already dark enough" and gives the faint small print no lift at all.

Measure it before you fix it

We did not trust our eyes for this. Two numbers helped:

  • Coverage in a text band: in a strip of small print, the share of pixels darker than a threshold. Broken letters show up as low coverage compared with a clean render of the same text.
  • Core lightness of a thin grey line: a printed grey rule should come out grey and whole, not white and not black.

Then we built synthetic test pages: render a page with known text, blur it by a known amount, add noise and a JPEG round trip, run the filter, and compare against the clean render. That turns "looks a bit broken" into a failing test you can fix against, and it stops a fix for soft text from quietly making sharp text look fake-bold.

What fixed it

The change that worked was to restore the stroke before the tone curve, per stroke, not per page:

  • A second sharpening pass at the blur's own scale, so the fringe is pulled back into the stroke before anything is pushed to white.
  • Re-measure how much each stroke needs after that restoration, instead of using one strength for the whole page. Shallow strokes get lifted, bold ones are left alone.
  • Shape the curve so a blurred stroke keeps its mid-tones (near linear), while a sharp stroke keeps the usual crisp core.
  • Gate it carefully: only near strokes, only on neutral (non-coloured) pixels so stamps and colour blocks are untouched, and not on dim, grainy photos where extra sharpening would just amplify noise.

On our soft test photo, the grey line went from badly broken to whole (its dark-pixel share rose by about half) and the bottom sentence went from pale grey to readable black, while sharp photos produced the same output as before. The honest cost: a faint speckle on empty paper that you can see at 2x zoom.

The camera side matters too

A filter can only rescue so much. Two camera changes reduced how often it has to:

  • Wait for focus before the shot. Lock focus at the page centre and give it a short time limit (we use well under a second), so the shutter still feels instant.
  • Check sharpness right after the shot. The variance of the Laplacian over the page area is a cheap, reliable blur score. If a shot is clearly soft, retake once automatically, then keep whatever comes next so the user is never stuck in a loop. A blank or very flat page should never be called soft.

Takeaways

  • When scans look broken, save the raw photo first. Often the photo is fine and the filter is the problem.
  • Blur widens strokes into grey fringes, and steep white-point curves delete those fringes.
  • Decide strength per stroke, not per page.
  • Write tests with synthetic blur levels so a fix for soft text cannot make sharp text worse.

We are working through this in the scanner of KovaPDF, our PDF app. If you want to see the web tools it sits next to, they are at https://www.kovapdf.com

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.