How to Unskew a Photo of a Document in Python
Document scanning applications benefit dramatically from OCR preprocessing steps. Thankfully, using only a few lines of complementary, ready-to-run Python code examples provided further down the page, you can take advantage of a free & powerful OCR preprocessing API which automatically detects and unskews a photo of a document (into a perfectly square image). Using this solution before any downstream OCR operations (especially those handling images from handheld cameras & smartphones) with improve the accuracy of that operation and lead to more consistent OCR results.
To install the Python SDK, first run the below command:
pip install cloudmersive-ocr-api-client
Next, add the imports and call the function:
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 unskew a photo of a document
api_response = api_instance.preprocessing_unskew(image_file)
pprint(api_response)
except ApiException as e:
print("Exception when calling PreprocessingApi->preprocessing_unskew: %s\n" % e)
To complete your API call & use this API for free, register a free account on our website and copy your free-tier API key into the configuration.api_key field above. Your free-tier key will supply a limit of 800 API calls per month with zero additional commitments upon reaching that limit.