如何在 Node Jimp 中將 Posterize 應用於影像?


NodeJS – Posterize() 是一個內建函式,用於將影像的色調等級降低到 'n' 級。n 將作為輸入引數。

語法

posterize(n, cb)

posterize() 引數的定義

  • n – 用於調整色調等級的輸入引數。最小值為 2。

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

輸入影像

使用 Node JIMP – POSTERIZE()

在使用 posterize() 函式之前,請確保已執行以下語句來設定環境。

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

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

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

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

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

示例

const Jimp = require('jimp') ;

async function posterize() {
   // Reading Image
   const image1 = await Jimp.read
   ('/home/jimp/tutorials_point_img.jpg');
   image1.posterize(5)
   .write('/home/jimp/posterize.jpg')
}

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

輸出

使用 Node JIMP – Posterze() 及 'cb' 引數

示例

const Jimp = require('jimp') ;

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

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

輸出

更新於: 2021年4月27日

273 次瀏覽

啟動您的 職業生涯

透過完成課程獲得認證

開始學習
廣告