Detect and Un-rotate a Document Image in Python

Before using a Cloudmersive OCR API to scan a document, it’s recommended to first run your document image through a preprocessing API to ensure the highest quality character recognition. With the /ocr/preprocessing/image/unrotated iteration of the OCR API, you can detect & unrotated an image of a document that was scanned or photographed at an unfortunate angle. This will improve the result quality of any subsequent OCR operation (check out our other OCR API articles to see what you can pair this iteration with in your application).

To connect using Python code, first start by running the below command:

pip install cloudmersive-ocr-api-client

After that, you can copy in the callback function. In the second snippet, include your Cloudmersive API key for authorization where indicated.

from __future__ import print_function
import time
import cloudmersive_ocr_api_client
from import ApiException
from pprint import pprint
# Configure API key authorization: Apikey
configuration = cloudmersive_ocr_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
# create an instance of the API class
api_instance = cloudmersive_ocr_api_client.PreprocessingApi(cloudmersive_ocr_api_client.ApiClient(configuration))
image_file = '/path/to/inputfile' # file | Image file to perform OCR on. Common file formats such as PNG, JPEG are supported.
# Detect and unrotate a document image
api_response = api_instance.preprocessing_unrotate(image_file)
except ApiException as e:
print("Exception when calling PreprocessingApi->preprocessing_unrotate: %s\n" % e)



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

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.