How to convert an image to black and white grayscale in Node.JS

Cloudmersive
1 min readApr 4, 2020

--

Black and white can be an excellent stylistic match for some images, bringing instant changes in emotional tone and emphasis. Making this conversion happen is simple enough with any photo editing program if you only have one or two images to deal with. When the numbers get a bit larger, this approach quickly becomes untenable. For an automated approach, we will turn to an API designed for Node.JS. Let’s dive in.

We must begin with our installation, completed with this reference for our package.json.

"dependencies": {     "cloudmersive-image-api-client": "^1.2.3"   }

Now our function can be called:

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';var apiInstance = new CloudmersiveImageApiClient.FilterApi();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.filterBlackAndWhite(imageFile, callback);

And we’re done! Seriously, it’s really that easy. Just input an image and it will be converted to grayscale.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet