How to Detect Large Text in an Image using Node.js
Before we make meaningful changes to image files, we should always know where important image subjects are located.
Thankfully, using the below code, we can easily take advantage of a free API in our Node.js applications which automatically detects the location of large text within our image files. This will return the X/Y coordinates of large text (based on our image’s pixel matrix), and it will also return the height and width of the text in question.
Let’s start by installing the SDK. We can either run the following command:
npm install cloudmersive-image-api-client --save
Or, alternatively, we can add the following snippet to our package.json:
"dependencies": {
"cloudmersive-image-api-client": "^1.3.4"
}
With that out of the way, we can now copy the below code into our file to complete our request. To authorize our requests for free, we’ll need a free-tier API key, which we can get by registering a free account on the Cloudmersive website:
var CloudmersiveImageApiClient = require('cloudmersive-image-api-client');
var defaultClient = CloudmersiveImageApiClient.ApiClient.instance;
// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';
var apiInstance = new CloudmersiveImageApiClient.RecognizeApi();
var imageFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Image file to perform the operation 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.recognizeDetectTextLarge(imageFile, callback);
Now we can quickly identify large text objects within our images and work around that information when we make changes — such as crops and other edits — to our images.