Convert an Image of Text into a Binarized (Light & Dark) View in Python
Before performing an OCR operation on an image of a document, it’s a good idea to do some preprocessing to ensure the document is as easily readable/recognizable as possible. Via the Cloudmersive API Console, you can leverage a suite of OCR Preprocessing APIs to companion the OCR API’s themselves (examples of OCR APIs have been covered in previous articles and include iterations for recognizing/converting PDF’s & images of business cards, receipts, and more).
With the /ocr/preprocessing/image/binarize iteration of the OCR API, you can perform an adaptive binarization on your input image to prepare it for further OCR. Take a look at how you can easily use this API in Python via copy & paste code from the Cloudmersive API Console.
First, install the Python SDK with the pip install command & start off your function with the following snippet:
pip install cloudmersive-ocr-api-clientfrom __future__ import print_function
from cloudmersive_ocr_api_client.rest import ApiException
from pprint import pprint
Next, authenticate your API key using the configuration snippet below. If you don’t currently have an API key, you can easily get one by making a free account on the Cloudmersive website.
# Configure API key authorization: Apikey
configuration = cloudmersive_ocr_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
Finally, complete the function with the last remaining snippet, and you’re good to go:
# 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:
# Convert an image of text into a binarized (light and dark) view
api_response = api_instance.preprocessing_binarize(image_file)
except ApiException as e:
print("Exception when calling PreprocessingApi->preprocessing_binarize: %s\n" % e)