如何在 Node Jimp 中翻轉影像?


NodeJS – flip() 是一個內建函式,用於翻轉影像。影像可以根據函式定義垂直或水平翻轉。預設情況下,影像會水平翻轉。

語法

flip(h, v, cb)

flip() 引數定義

  • h – 它接收一個布林值作為輸入。如果為 TRUE,則影像將水平翻轉。

  • v – 它接收一個布林值作為輸入。如果為 TRUE,則影像將垂直翻轉。

  • cb – 這是一個可選引數,可以在編譯完成後呼叫。

輸入影像

使用 Node JIMP – flip()

在繼續使用 flip() 函式之前,請檢查以下語句是否已執行以設定環境。

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

  • npm install jimp --save // 安裝 jimp 依賴項

  • 建立一個 flip.js 檔案,並將以下程式碼片段複製貼上到其中。

  • 使用 node flip.js 執行程式碼。

注意 - 請注意,方法名稱應與 JS 檔名稱匹配。只有這樣,它才能呼叫所需的方法。

示例

const Jimp = require('jimp') ;

async function flip() { // Function name is same as of file name
   // Reading Image
   const image = await Jimp.read
   ('/home/jimp/tutorials_point_img.jpg');
   image.flip(true, false, function(err){
      if (err) throw err;
   })
   .write('/home/jimp/flip.jpg');
}

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

輸出

當 'v' 為 TRUE 時使用 Node JIMP – flip()

示例

const Jimp = require('jimp') ;

async function flip() {
   // Reading Image
   const image = await Jimp.read
   ('/home/jimp/tutorials_point_img.jpg');
   image.flip(true, true, function(err){
      if (err) throw err;
   })
   .write('/home/jimp/flip.jpg');
}

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

輸出

更新於: 2021年4月27日

602 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告