Deskew Images in Python | Image Skew Correction in Python

If you are looking for a way to correctly align the tilted content in images, then you have landed in the right place. In this article, we will learn how to perform skew correction in image processing using Python. We will walk you through the basics of skew correction in Python. We’ll cover how to detect skew in an image, how to calculate the angle of skew, and how to deskew your images programmatically using Python.

This article covers the following topics:

  1. Image Skew Correction
  2. Python Skew Correction API
  3. Skew Detection in Python
  4. Automatic Skew Correction
  5. DeSkew Images Manually
  6. Skew Correction - Free Resources

Image Skew Correction

Skew correction in image processing, also known as deskewing, is the process of aligning an image that is tilted or skewed to make it appear more visually appealing and easier to work with. Skew can occur due to various reasons, such as improper scanning, perspective distortion, or camera misalignment, as shown in the below image:

Skewed Image

The source image.

Python API to Deskew Images

Aspose.OCR is a widely used OCR library that allows performing OCR on scanned images, smartphone photos, screenshots, areas of an image, and scanned PDFs in various programming languages, including Python. We will be using the Aspose.OCR for Python via .NET API for skew corrections.

Please download the package or install the API from PyPI using the following pip command in the console:

pip install aspose-ocr-python-net

Detect Skew Angle in Python

We can easily detect the skew angle of a text in an image by following the steps below:

  1. Create an instance of the AsposeOCR class.
  2. Initialize an object of the OcrInput class.
  3. Add the image to the OcrInput recognition batch.
  4. Call the calculate_skew() method. It takes the image path as an argument.
  5. Show the calculated skew angle.

The following sample code shows how to detect the skew angle of an image in Python.

File: D:\Files\source.png
Skew angle: 5.8°

Automatic Skew Correction in Python

We can enable automatic skew correction and save the corrected image by following the steps below:

  1. Create an instance of the AsposeOCR class.
  2. Initialize an object of the PreprocessingFilter class.
  3. Add the auto_skew() filter.
  4. After that, add the image to the recognition batch and apply a processing filter using the OcrInput class.
  5. Finally, call the ImageProcessing.save() method to save the processed image to the specified path folder.

The following sample code shows how to apply auto-skew correction with a preprocessing filter in Python.

Automatic Skew Correction with Filter in Python

Automatic Skew Correction with Filter in Python.

Deskew Images Manually in Python

For manual skew correction, we can define the skew angle manually by following the steps below:

  1. Create an instance of the AsposeOCR class.
  2. Initialize an object of the PreprocessingFilter class.
  3. Add the rotate() filter.
  4. After that, add the image to the recognition batch and apply a processing filter using the OcrInput class.
  5. Finally, call the ImageProcessing.save() method to save the processed image to the specified path folder.

The following sample code shows how to deskew images manually in Python.

Get a Free License

You can get a free temporary license to try Aspose.OCR for Python via .NET without evaluation limitations.

Skew Correction - Free Resources

Besides implementing image skew correction in Python, you may further explore the Python OCR API using the following resources:

Conclusion

In this article, we have learned how to correctly align the tilted images using the image skew correction in Python. We have seen how to deskew images using the automatic method and by setting the rotation angle manually. By leveraging Aspose.OCR for Python via .NET, you can easily embed such a feature in your Python applications. In case of any ambiguity, please feel free to contact us on our free support forum.

See Also