Alyn Save

Detect and fix skew in images containing text

Project README


PyPI version

Skew detection and correction in images containing text

Image with skew
Image after deskew

Install and use via pip!

Recommended way(using virtualenv):

  1. mkdir alyn-test
  2. cd alyn test
  3. mkvirtualenv .
  4. pip install alyn
  5. source bin/activate

To detect skew angle in image:

from alyn import SkewDetect
sd = SkewDetect(
Extra options:
  • sigma:canny edge detection blurring
  • plot_hough: display hough lines detected
  • num_peaks: control the number of hough line peaks

To deskew image:

from alyn import Deskew
d = Deskew(
	display_image='preview the image on screen',
	output_file='path_for_deskewed image',


  • numpy
  • matplotlib
  • scipy
  • scikit-image

Techniques used

  • Canny Edge Detection
  • Hough Transform


  • Detect the skew in given images
  • Display the output
  • Save the output to txt file
  • Batch process files in a directory
  • View Hough Transform of a given image
  • Set the number of peaks for Hough Transform and Sigma for Canny Edge detection
  • Rotate the image to remove the skew

How the skew detection works

The skew detection script takes image file as input, then performs the following steps:

  • Converts the image to greyscale
  • Performs Canny Edge Detection on the Image
  • Calculates the Hough Transform values
  • Determines the peaks
  • Determines the deviation of each peaks from 45 degree angle
  • Segregates the detected peaks into bins
  • Chooses the probable skew angle using the value in the bins

The deskew script uses the skew angle determined using skew detection script to remove the skew from the image.

Using scripts directly(older method)

Image skew calculation using

To calculate the skew angle for a given image file, use -i option followed by the path to file:

./ -i image.jpg

To save output in a text file add -o option followed by the output file name:

./ -i image.jpg -o output.txt

To display output information add -d option followed by a string Yes:

./ -i image.jpg -d Yes

To batch process files in a directory, use -b option followed by the path to directory:

./  -b examples

To display Hough Transform plot for an image,:

./ -i image.jpg -p Yes
Output of the Hough Transform:

To set the value of sigma for Gaussian blurring in Canny Edge Detection, use -s option followed by the desired value:

./ -i image.jpg -s 3

To set the number of peaks collected from Hough Transform, use -n option followed by the desired value:

./ -i image.jpg -n 10

Image Deskew using

To perform a simple deskew and display the output:

./ -i image.jpg -d Yes

To save the deskewed image, use the following:

./ -i image.jpg -o rotated.jpg

In some cases the result image might be upside down or the text may be running vertical, To fix this, use -r followed by the desired angle in int:

./ -i image.jpg -o rotated.jpg -r 90

To generate data for experimental purposes, run the in test_data folder. This will generate images containing a white line having angle between 0 to 180 degrees.

Open Source Agenda is not affiliated with "Alyn" Project. README Source: kakul/Alyn
Open Issues
Last Commit
4 years ago

Open Source Agenda Badge

Open Source Agenda Rating