How to blur an image using Node Jimp blur() function?

Node.jsServer Side ProgrammingProgramming

NodeJS – Blur() is an inbuilt function that is used to blur images. The effect produced by JIMP blur() will be similar to that of the Gaussian blur. It blurs based on the pixels input.

Syntax

blur(r, cb)

Definition of blur() paramters

  • r – It takes the input for blur and blurs based upon the pixels. If r is 2, it will blur 2 pixels at a time.

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

Input Image

Using Node JIMP – BLUR()

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

  • npm init -y // Initialising the Node environment

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

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

  • Use node blur.js to run the code.

Note – Note that 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 blur() { // Function name is same as of file name
   // Reading Image
   const image = await Jimp.read
   ('/home/jimp/tutorials_point_img.jpg');
   image.blur(3)
   .write('/home/jimp/blur.jpg');
}

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

Output

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

Example

const Jimp = require('jimp') ;

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

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

Output

raja
Published on 27-Apr-2021 11:31:45
Advertisements