sbrunner/deskew

Library used to deskew a scanned document

What it solves

Scanned or photographed documents often have text that is tilted. This tool detects the angle of that tilt and rotates the image to straighten the text.

How it works

The library measures the skew angle from a grayscale version of the image. You can use it as a Python function or as a command-line tool. The returned angle is kept between -45 and 45 degrees to avoid flipping the image orientation, unless you opt for a wider range. After getting the angle, you rotate the image yourself (examples show scikit-image and OpenCV). The tool also offers debug images to help tune detection parameters if results are poor.

Who it's for

Developers and anyone working with document images who need to preprocess them for OCR or display, and who use Python.

Highlights

  • Provides a simple Python function determine_skew to get the angle.
  • Includes a command-line interface: deskew input.png prints the angle, deskew --output out.png input.png saves the corrected image.
  • Can return an angle between -45 and 45 degrees by default, or -90 to 90 with the angle_pm_90 flag.
  • Offers debug image generation (via the optional [debug_images] install) to help tune parameters like num_peaks, min_angle, max_angle, etc.
  • Inspired by the Alyn tool.

Related

  • Project
  • Project
  • Project
  • Project