How to Convert an Image of Text into a Binary View using Node.js

Cloudmersive
2 min readNov 13, 2023

--

Enhancing the contrast between light and dark areas in a document image makes downstream OCR operations easier to carry out.

Using the below code, we can improve our OCR applications easily by calling a free preprocessing API before our image-to-text conversion workflow. This API service will use machine learning — specifically, a deep learning binarization algorithm — to convert our document image into a binary (light and dark) view.

Calling this service is much easier than creating the logic from scratch, and API calls can be authorized freely with a free-tier API key. Free-tier API keys allow a limit of 800 API calls per month with no additional commitments.

To get started, we can first run the below command to install the SDK:

npm install cloudmersive-ocr-api-client --save

Alternatively, we can elect to install the SDK by adding the following snippet to our package.json instead:

  "dependencies": {
"cloudmersive-ocr-api-client": "^1.3.3"
}

Finally, we can use the below code to call the function & make the request with our image file path:

var CloudmersiveOcrApiClient = require('cloudmersive-ocr-api-client');
var defaultClient = CloudmersiveOcrApiClient.ApiClient.instance;

// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';



var apiInstance = new CloudmersiveOcrApiClient.PreprocessingApi();

var imageFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Image file to perform OCR on. Common file formats such as PNG, JPEG are supported.


var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.preprocessingBinarizeAdvanced(imageFile, callback);

No more code required — we’re all set!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet