How to apply Posterize to an image in Node Jimp?

Node.jsServer Side ProgrammingProgramming

NodeJS – Posterize() is an inbuilt function that is used to apply posterize to images upto the level 'n'. The n will be the input parameter.

Syntax

posterize(n, cb)

Definition of posterize() paramters

  • n – It will take input to adjust the posterize level. Minimum value possible is 2.

  • cb – This is an optional parameter that can be invoked after the compilation is complete.

Input Image

Using Node JIMP – POSTERIZE()

Before proceeding to use posterize() functions, please check that the following statements are already executed for setting up the environment.

  • npm init -y // Initialising the Node environment

  • npm install jimp --save // Installing the jimp dependency

  • Create a posterize.js file and copy-paste the following code snippet in it.

  • Use node posterize.js to run the code.

Note − The method name should match with the JS file name. Only then it will be able to call the desired method.

Example

const Jimp = require('jimp') ;

async function posterize() {
   // Reading Image
   const image1 = await Jimp.read
   ('/home/jimp/tutorials_point_img.jpg');
   image1.posterize(5)
   .write('/home/jimp/posterize.jpg')
}

posterize(); // Calling the function here using async
console.log("Image is processed successfully");

Output

Using Node JIMP – Posterze() with 'cb' parameters

Example

const Jimp = require('jimp') ;

async function posterize() {
   // Reading Image
   const image = await Jimp.read
   ('/home/jimp/tutorials_point_img.jpg');
   image.posterize(10, function(err){
      if (err) throw err;
   })
   .write('/home/jimp/posterize.jpg');
}

posterize();
console.log("Image is processed successfully");

Output

raja
Published on 27-Apr-2021 13:44:32
Advertisements