Detect and Un-rotate a Document Image in Python

Cloudmersive
2 min readFeb 17, 2022

--

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 cloudmersive_ocr_api_client.rest 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.
try:
# Detect and unrotate a document image
api_response = api_instance.preprocessing_unrotate(image_file)
pprint(api_response)
except ApiException as e:
print("Exception when calling PreprocessingApi->preprocessing_unrotate: %s\n" % e)

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet