如何在 Node Jimp 中将 Posterize 应用于图像?
NodeJS – Posterize() 是一个内置函数,用于将图像的色调等级降低到 'n' 级。n 将作为输入参数。
语法
posterize(n, cb)
posterize() 参数的定义
n – 用于调整色调等级的输入参数。最小值为 2。
cb – 这是一个可选参数,可以在编译完成后调用。
输入图像

使用 Node JIMP – POSTERIZE()
在使用 posterize() 函数之前,请确保已执行以下语句来设置环境。
npm init -y // 初始化 Node 环境
npm install jimp --save // 安装 jimp 依赖项
创建一个名为 posterize.js 的文件,并将以下代码片段复制粘贴到其中。
使用 node posterize.js 运行代码。
注意 - 方法名称应与 JS 文件名匹配。只有这样才能调用所需的方法。
示例
const Jimp = require('jimp') ;
async function posterize() {
// Reading Image
const image1 = await Jimp.read
('/home/jimp/tutorials_point_img.jpg');
image1.posterize(5)
.write('/home/jimp/posterize.jpg')
}
posterize(); // Calling the function here using async
console.log("Image is processed successfully");输出

使用 Node JIMP – Posterze() 及 'cb' 参数
示例
const Jimp = require('jimp') ;
async function posterize() {
// Reading Image
const image = await Jimp.read
('/home/jimp/tutorials_point_img.jpg');
image.posterize(10, function(err){
if (err) throw err;
})
.write('/home/jimp/posterize.jpg');
}
posterize();
console.log("Image is processed successfully");输出

广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP