节点吉普 |分色
简介: posterize()函数是 Nodejs 中的内置函数| Jimp 应用n级的分色效果。
句法:
posterize(n, cb)
范围:
- n - 此参数存储调整对比度的量,最小阈值为两个
- cb – 这是编译完成时调用的可选参数。
输入图像:
第一步:搭建环境
npm init -y
第 2 步:安装jimp
npm install jimp --save
示例 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');
// sepia function
image.posterize(4)
.write('posterize1.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');
// opaque function using callback function
image.posterize(5, function(err){
if (err) throw err;
})
.write('posterize2.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');
// opaque function using callback function
image.posterize(5, function(err){
if (err) throw err;
})
.write('posterize2.png');
}
main();
console.log("Image Processing Completed");
输出:
参考: https://www.npmjs.com/package/jimp