Schema Tax

How to Deskew a Scanned Document Before OCR

By Schema TaxPublished Updated

To deskew a scanned document before OCR is to rotate the page image until its text lines run perfectly level. Do it because OCR finds lines and boxes by assuming they are horizontal; a tilted page lets values slide toward the wrong row. Measure the angle, rotate by the opposite amount, crop, and check one row before running OCR.

What skew does to OCR on a scanned tax document

Skew is the angle between a scanned page's text lines and the true horizontal. On a scanned tax document, skew makes each row of boxes slope, so a value at the right end sits lower or higher than its label. OCR that reads row by row can then attach that value to the wrong box.

The Tesseract OCR engine's documentation on improving the quality of the output puts it plainly: "The quality of Tesseract's line segmentation reduces significantly if a page is too skewed, which severely impacts the quality of the OCR." Line segmentation is the step that splits a page into lines of text before any character is read. If the lines come out wrong, every character after it is in the wrong place.

A before-and-after example at 3 degrees

Here is an example with approximate numbers. Picture a W-2 fed into a sheet scanner at a 3-degree clockwise tilt. On the IRS Form W-2, the numbered boxes sit in two columns on the right half of the form: Box 1 beside Box 2, Box 3 beside Box 4, Box 5 beside Box 6.

A 3-degree slope drops about 0.05 inches for every inch across. Over the roughly 4-inch width of the numbered boxes, the right edge sits about 0.2 inches lower than the left. That is a large share of one box row's height.

Before deskewing, in this example:

  • Box 2's amount, say $4,212.00, sits low enough to touch the top of Box 4's area.
  • Box 4's amount, say $3,224.00, edges toward Box 6.
  • The state and local row at the bottom, Boxes 15 to 20, runs the full width of the form, so its right end drops about twice as far, and a Box 19 local tax amount can read as part of the line below.

After deskewing, each row is level again. Box 1 and Box 2 share one baseline, and every amount sits fully inside its own box. Nothing about the characters changed; only their positions did.

Small tilts are the dangerous ones. A page tilted 20 degrees looks wrong to anyone. A page tilted 2 or 3 degrees looks fine at a glance, gets uploaded, and produces values that are each read correctly but filed under the wrong box.

How to deskew a scanned tax document

Deskewing a scanned tax document takes four steps: measure the skew angle from a long straight line on the page, rotate the image by the opposite angle, crop away the triangles of background the rotation leaves, and check that one full row of boxes is level. Most image tools and OCR tools can do the first two steps automatically.

The four-step procedure

  1. Measure the angle. Pick the longest straight line on the page, such as the top border of the form or a full-width row divider. Compare where it sits at the left and right edges. Automatic tools do this by testing angles until text lines line up.
  2. Rotate by the opposite angle. If the page slopes down 3 degrees to the right, rotate it 3 degrees counterclockwise. Rotate the original image once; repeated small rotations soften the digits a little each time.
  3. Crop the edges. Rotation leaves wedges of blank or black background at the corners. Crop them, leaving a thin white margin around the form. The Tesseract guide to improving the quality of the output warns that dark scanner borders "can be erroneously picked up as extra characters."
  4. Check one row. Zoom in on a full row of boxes, such as Box 1 and Box 2 on a W-2. Both amounts should sit on the same baseline, inside their own borders.

On the command line, ImageMagick has a straightening option. Its command-line options reference describes -deskew as "Straighten an image. A threshold of 40% works for most images," with a setting to auto crop the result. A typical call looks like this:

magick scan.png -deskew 40% scan-straight.png

Many scanner drivers and PDF editors offer the same thing as a "straighten" or "auto-deskew" checkbox. The settings that reduce skew at scan time, and the other values worth setting, are in the guide to scanner settings for tax documents.

Does OCR software deskew a scanned document for you?

Many OCR and extraction tools deskew a scanned document for you, detecting the angle and rotating the page before reading. Whether yours does depends on the tool and its settings. Check its documentation, or test it with a page you tilt on purpose, before you add a manual deskew step to every scan.

The quick test: scan one W-2 at a deliberate small tilt, run it through the tool, and compare the extracted values with the paper. If Box 2 and Box 4 come back correct and in the right fields, the tool straightened it. If amounts appear under the wrong field names, add your own deskew step first.

Schema Tax straightens crooked scans before it reads them, then flags any box it is unsure of for you to confirm rather than guessing. The five steps from paper to checked data show where that fits.

Scanner apps on phones often deskew too, as part of correcting perspective. Prevention still beats correction. The guide on holding the phone flat over a tax document covers the capture habits that keep tilt small in the first place.

When choosing tools, automatic deskew is one of the features worth checking. The comparison of tax document scanning software covers it alongside other capture features.

When deskewing a scanned document is not enough?

Deskewing a scanned document fixes rotation only. It cannot fix a page that is curled, photographed at an angle that turns the rectangle into a wedge, or cropped so that boxes are missing. Those problems need a different correction or a new scan. Rotation keeps straight lines straight; these faults bend or remove them.

Curled pages. A page that bows near the spine of a folder or a staple makes rows curve. Rotating a curve does not straighten it; one end stays off. Flatten the page and rescan.

Keystone distortion. Keystone is the wedge shape a page takes when the camera is not parallel to it. The near edge looks wider than the far edge. Rotation cannot fix this, but perspective correction can, which is what phone scanner apps do when you adjust the four corners.

Cropped edges. If the scan cut off the right side of a W-2 or its bottom state lines, those boxes are not in the image. No processing recovers them. Rescan with a margin.

Mixed angles. A form taped onto another page, or a receipt stapled to a statement, can sit at its own angle. Deskewing the page straightens one and tilts the other. Scan each item on its own.

After any correction, run the same check: one level row, every amount inside its own box. Validation checks then catch some leftovers, such as a W-2 Box 4 that no longer matches 6.2% of Box 3, the relationship worked in the IRS General Instructions for Forms W-2 and W-3, because a digit drifted. The accuracy and security category covers what those checks can and cannot catch.

All posts in Tax Form OCR Basics