节点吉普 |不透明度
简介: opacity()函数是 Nodejs 中的内置函数| Jimp 将每个像素的不透明度乘以 0 到 1 之间的因子。
句法:
opacity(f, cb)
范围:
- f - 此参数存储图像要使不透明的值。它的范围为 0 到 1。1 将使图像完全透明。
- 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
('https://media.geeksforgeeks.org/wp-content/uploads/20190328185307/gfg28.png');
// opacity function
image.opacity(.3)
.write('opacity1.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');
// opacity function using callback function
image.opacity(.5, function(err){
if (err) throw err;
})
.write('opacity2.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');
// opacity function using callback function
image.opacity(.5, function(err){
if (err) throw err;
})
.write('opacity2.png');
}
main();
console.log("Image Processing Completed");
输出:
参考: https://www.npmjs.com/package/jimp