How to fix a skewed document photo in Python

Having support for phone camera images in your optical character recognition can really expand your options. Unfortunately, this also comes with the headache of dealing with skewed and crooked images. To prevent this from ruining your OCR accuracy, it is necessary to first perform the appropriate pre-processing passes upon the image. Generally this involves a lot of deep learning, time, and problems. Well, it’s time you count your lucky stars, because I’m going to show you how to get it done in a matter of moments. Here’s how.

We will be using an API to get these rapid-fire results, and so we will need its client to be installed:

pip install cloudmersive-ocr-api-client

Now we will need to set up our function call for preprocessing_unskew.

from __future__ import print_functionimport timeimport cloudmersive_ocr_api_clientfrom cloudmersive_ocr_api_client.rest import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_ocr_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_ocr_api_client.PreprocessingApi(cloudmersive_ocr_api_client.ApiClient(configuration))image_file = '/path/to/file' # file | Image file to perform OCR on.  Common file formats such as PNG, JPEG are supported.try:# Detect and unskew a photo of a documentapi_response = api_instance.preprocessing_unskew(image_file)pprint(api_response)except ApiException as e:print("Exception when calling PreprocessingApi->preprocessing_unskew: %s\n" % e)

As you may have noticed, we can input our image_file, which will be sent on to the API and then back to us, nice and straight. And there you have it, easy as pie.

Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store