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
import time
import cloudmersive_ocr_api_client
from 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.
# 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)




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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

10 free stock photos you would actually use (Monday 23rd 08PM edition)

Creating Custom Template Tags In Django Application

V2-Android StopWatch ( New Era of Rx )

How to Solve “Outlook PST is not a personal folders file” Error

How to Solve “Outlook PST is not a personal folders file” Error

Using Drones to Create Beautiful 3D Context Models

Increase Value, Agility and Scalability with AWS Serverless

Improving Code Quality in Python Codebases


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.

More from Medium

Detect and Un-rotate a Document Image in Python

Face Recognition Based Attendance System with GUI Using Opencv and Tkinter

Object Detection made simpler with IceVision (Part-1)

Head Pose Estimation Using Geometric Model