节点吉普 |颜色灰度
介绍
灰度修改器是 Nodejs 中内置的颜色修改器 | Jimp 根据给定的数量(从 0 到 100)将图像颜色去饱和为灰度。
句法:
image.color([
{ apply: 'grayscale', params: [value] }
]);
范围:
- value - 此参数存储要应用的灰度量。它的取值范围为 0 – 100。
输入图像:
示例 1:
javascript
// npm install --save jimp
// import jimp library to the environment
var Jimp = require('jimp');
//User-Defined Function to read the images
async function main() {
const image = await Jimp.read
('https://media.geeksforgeeks.org/wp-content/uploads/20190328185307/gfg28.png');
// color function having grayscale modifier
image.color([{apply:'grayscale', params: [50]}])
.write('grayscale1.png');
}
main();
console.log("Image Processing Completed");
javascript
//npm install --save jimp
//import jimp library to the environment
var Jimp = require('jimp');
//User-Defined Function to read the images
async function main() {
const image = await Jimp.read
('https://media.geeksforgeeks.org/wp-content/uploads/20190328185333/gfg111.png');
// color function having grayscale modifier
image.color([{apply:'grayscale', params: [50]}], function(err){
if (err) throw err;
})
.write('grayscale2.png');
}
main();
console.log("Image Processing Completed");
输出:
示例 2:cb(可选参数)
javascript
//npm install --save jimp
//import jimp library to the environment
var Jimp = require('jimp');
//User-Defined Function to read the images
async function main() {
const image = await Jimp.read
('https://media.geeksforgeeks.org/wp-content/uploads/20190328185333/gfg111.png');
// color function having grayscale modifier
image.color([{apply:'grayscale', params: [50]}], function(err){
if (err) throw err;
})
.write('grayscale2.png');
}
main();
console.log("Image Processing Completed");
输出:
参考: https://www.npmjs.com/package/jimp