Open In App

Node Jimp | posterize

The posterize() function is an inbuilt function in Nodejs | Jimp that applies a posterize effect with n level. 

Syntax: 



posterize(n, cb)

Parameter: 

Input Images: 



Step 1: Setting up the environment 

npm init -y

Step 2: Install jimp

npm install jimp --save

Example 1:In this example, we will apply the posterize effect.




// npm install --save jimp
// import jimp library to the environment
const Jimp = require('jimp');
 
// User-Defined Function to read the images
async function main() {
    const image = await Jimp.read(
    // sepia function
    image.posterize(4)
        .write('posterize1.png');
}
 
main();
console.log("Image Processing Completed");

Output: 


Example 2: With cb (optional parameters) 




// npm install --save jimp
// import jimp library to the environment
const Jimp = require('jimp');
 
// User-Defined Function to read the images
async function main() {
    const image = await Jimp.read(
    // opaque function using callback function
    image.posterize(5, function (err) {
        if (err) throw err;
    })
        .write('posterize2.png');
}
 
main();
console.log("Image Processing Completed");

Output: 


Reference: https://www.npmjs.com/package/jimp


Article Tags :