节点吉普 |像素化
简介:pixelate()函数是 Nodejs 中的内置函数| Jimp 在图像或区域上应用像素化效果。
句法:
pixelate(size, x, y, w, h, cb)
范围:
- size - 此参数采用像素的大小。
- x - 此参数采用要像素化的区域的 x 位置。
- y – 此参数采用要像素化的区域的 y 位置。
- w - 这是一个可选参数,它采用像素化区域的宽度。
- h - 这是一个可选参数,它采用像素化区域的高度。
- cb – 这是编译完成时调用的可选参数。
输入图像:
设置环境:
npm init -y
安装依赖:
npm install jimp
示例 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('../gfg.png');
// pixelate function
image.pixelate(5)
.write('pixelate1.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('../gfg1.png');
//pixelate function using callback function
image.pixelate(5, 40, 50, 190, 200, function(err){
if (err) throw err;
})
.write('pixelate2.png');
}
main();
console.log("Image Processing Completed");
输出:
示例 2:带大小、x、y、w、h 和 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('../gfg1.png');
//pixelate function using callback function
image.pixelate(5, 40, 50, 190, 200, function(err){
if (err) throw err;
})
.write('pixelate2.png');
}
main();
console.log("Image Processing Completed");
输出:
参考: https://www.npmjs.com/package/jimp