Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to change an image to greyscale in Node Jimp?
NodeJS – Grayscale() is an inbuilt function that is used to desaturate an image color into gray depending upon the amount between 0 to 100. The grayscale is basically the conversion of an image into gray from their original colors.
Syntax
image.color([
{apply: 'greyscale', params: [value], cb }
]);
Definition of greyscale() paramters
value – It takes the input for the parameter value to apply the greyscale. The range will be from 0 to 100.
cb – This is an optional parameter that can be invoked after the compilation is complete.
Input Image

Using Node JIMP – GREYSCALE()
Before proceeding to use greyscale() 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 greyscale.js file and copy-paste the following code snippet in it.
Use node greyscale.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 greyscale() { // Function name is same as of file name
// Reading Image
const image = await Jimp.read
('/home/jimp/tutorials_point_img.jpg');
image.grayscale ()
.write('/home/jimp/greyscale.jpg');
}
greyscale(); // Calling the function here using async
console.log("Image is processed successfully");
Output

Using Node JIMP – GREYSCALE() with 'cb' parameters
Example
const Jimp = require('jimp') ;
async function greyscale() {
// Reading Image
const image = await Jimp.read
('/home/jimp/tutorials_point_img.jpg');
image.color([{apply:'greyscale', params: [90]}], function(err){
if (err) throw err;
})
.write('/home/jimp/greyscale.jpg');
}
greyscale();
console.log("Image is processed successfully");
Output

