如何在 NodeJS 中使用 Jimp 旋转图像色相?


NodeJS 的 `spin` 修饰符是一个内置函数,用于将图像的色相旋转给定数量(-360° 到 360°)。我们可以将颜色旋转 0 到 360 度,但这不会执行任何功能,因为它将色相设置为之前的状态。

语法

image.color([
   {apply: 'spin', params: [value] }
]);

color() 参数

  • value – 它将存储旋转时要应用的色相量。其可能值为 -360 到 360。

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

输入图像

使用 Node JIMP – COLOR – SPIN

在使用 color() - spin 函数之前,请检查以下语句是否已执行以设置环境。

  • npm init -y // 初始化 Node 环境

  • npm install jimp --save // 安装 jimp 依赖项

  • 创建一个 colorSpin.js 文件并将以下代码片段复制粘贴到其中。

  • 使用 node colorSpin.js 运行代码。

注意 − 请注意,方法名称应与 JS 文件名匹配。只有这样才能调用所需的方法。

示例

const Jimp = require('jimp') ;

async function colorSpin() {
   // Reading Image
   const image = await Jimp.read
   ('/home/jimp/tutorials_point_img.jpg');
   image.color([{apply: 'spin', params: [50]}])
   .write('/home/jimp/colorSpin.jpg');
}

colorSpin(); // Calling the function here using async
console.log("Image is processed successfully");

输出

使用 Node JIMP – Color - SPIN 带 'cb' 参数

示例

const Jimp = require('jimp') ;

async function colorSpin() {
   // Reading Image
   const image = await Jimp.read
   ('/home/jimp/tutorials_point_img.jpg');
   image.color([{apply:'spin', params: [100]}], function(err){
      if (err) throw err;
   })
   .write('/home/jimp/colorSpin.jpg');
}

colorSpin();
console.log("Image is processed successfully");

输出

更新于:2021年4月27日

256 次浏览

开启你的职业生涯

完成课程获得认证

开始学习
广告