How to invert the colors of an image in Node Jimp?

Node.jsServer Side ProgrammingProgramming

NodeJS – Invert() is an inbuilt function that is used to invert the image colors.

Syntax

invert(cb)

sepia() paramters

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

Input Image

Using Node JIMP – INVERT()

Before proceeding to use invert() 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 an invert.js file and copy-paste the following code snippet in it.

  • Use node invert.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 invert() {
   // Reading Image
   const image= await Jimp.read
   ('/home/jimp/tutorials_point_img.jpg');
   image.invert()
   .write('/home/jimp/invert.jpg')
}

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

Output

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

Example

const Jimp = require('jimp') ;

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

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

Output

raja
Published on 27-Apr-2021 13:36:10
Advertisements