节点吉普 |翻动
简介: flip()函数是 Nodejs 中的内置函数| Jimp 水平或垂直翻转图像。默认设置是水平翻转图像。
句法:
flip(h, v, cb)
范围:
- h - 此参数采用布尔值,如果为 true,则图像将水平翻转。
- v - 此参数采用布尔值,如果为 true,则图像将垂直翻转。
- 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');
// flip function also known as mirror function
image.flip(true, false)
.write('flip1.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');
// flip function using callback function
image.flip(true, true, function(err){
if (err) throw err;
})
.write('flip2.png');
}
main();
console.log("Image Processing Completed");
输出:
示例 2:使用 h、v 和 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');
// flip function using callback function
image.flip(true, true, function(err){
if (err) throw err;
})
.write('flip2.png');
}
main();
console.log("Image Processing Completed");
输出:
参考: https://www.npmjs.com/package/jimp