How to remove transparency from an image in Node.JS

Transparent images can definitely cause their share of problems in the wrong circumstances. Removing transparency should be as simple as rendering the image over a white background, right? Well, in practice it actually requires quite a bit more effort than that. Instead of going into that whole process, how about we just use an API instead?

We will be installing our API client with this command for npm install.

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

After installation finishes, we can call this function from our API.

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 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.editRemoveTransparency(imageFile, callback);

Now we just need to test it out on a transparent image to ensure it’s working properly. It’s really as simple as that.

Image for post
Image for post

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