Open In App

Node Jimp | posterize

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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

Syntax: 

posterize(n, cb)

Parameter: 

  • n: This parameter stores the amount to adjust the contrast and the minimum threshold is two
  • cb: This is an optional parameter that is invoked when compilation is complete.

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.

javascript




// 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) 

javascript




// 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



Last Updated : 27 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads