如何在 NodeJS 中使用 Jimp 旋轉影像色相?


NodeJS 的 `spin` 修飾符是一個內建函式,用於將影像的色相旋轉給定數量(-360° 到 360°)。我們可以將顏色旋轉 0 到 360 度,但這不會執行任何功能,因為它將色相設定為之前的狀態。

語法

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

color() 引數

  • value – 它將儲存旋轉時要應用的色相量。其可能值為 -360 到 360。

輸入影像

使用 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 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.