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:
- 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( 'https://media.w3wiki.net/wp-content/uploads/20190328185307/gfg28.png' ); // 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( 'https://media.w3wiki.net/wp-content/uploads/20190328185333/gfg111.png' ); // 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