How to posterize an image by reducing colors in Node.JS

A great way to add some visual interest and style to an image is through posterization, or reducing colors found in an image down to a select few, giving kind of a retro look. We will be approaching this a little different than the normal method, instead opting for use of an API which will get us the results we need without much effort at all. Let’s get started.

First comes installation of our client through npm install:

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

From there we can continue with calling our function, like so:

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.FilterApi();var levels = 56; // Number | Number of unique colors to retain in the output imagevar 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.filterPosterize(levels, imageFile, callback);

Now let’s pick a number for our color levels, then input our image. And that’s about all there is to it. Let’s test it out on this image:

Image for post
Image for post

And here is our image after posterization:

Image for post
Image for post

Written by

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