How to adaptively adjust the contrast of an image in Node.JS

A few quick adjustments can spell the difference between a bright, standout image and something dull and forgettable. When working with large numbers of images, it’s necessary to have an automated system that can adaptively adjust the contrast specifically for each image. We are going to make this super easy today by using an API that’s already set up to examine images and pick the appropriate contrast setting.

We start with installation of our client. Npm install will get this done for us in no time:

npm install cloudmersive-image-api-client --save

Once our installation has been carried out, we will now have access to the full range of functions contained within the API. We will be using editContrastAdaptive:

var CloudmersiveImageApiClient = require('cloudmersive-image-api-client');var defaultClient = CloudmersiveImageApiClient.ApiClient.instance;// Configure API key authorization: Apikeyvar Apikey = defaultClient.authentications['Apikey'];Apikey.apiKey = 'YOUR API KEY';// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.apiKeyPrefix = 'Token';var apiInstance = new CloudmersiveImageApiClient.EditApi();var gamma = 1.2; // Number | Gamma value to adjust the contrast in the image.  Recommended value is 2.0.  Values between 0.0 and 1.0 will reduce contrast, while values above 1.0 will increase contrast.var imageFile = "/path/to/file"; // 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.editContrastAdaptive(gamma, imageFile, callback);

Now that it’s set up, anything we feed in as our imageFile will be assessed, adjusted, and returned, just like that. Easy.

Image for post
Image for post

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

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