如何在 Node Jimp 中使用 contrast() 函式調整影像對比度?


NodeJS – contrast() 是一個內建函式,用於調整影像的對比度。它會根據輸入值(範圍為 -1 到 +1)增加或減少對比度。

語法

contrast(n, cb)

contrast() 引數的定義

  • – 它將接收 n 作為輸入來調整影像的對比度,n 的可能值介於 -1 和 +1 之間。

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

輸入影像

使用 Node JIMP – CONTRAST()

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

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

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

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

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

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

示例

const Jimp = require('jimp') ;

async function contrast() { // Function name is same as of file name
   // Reading Image
   const image = await Jimp.read
   ('/home/jimp/tutorials_point_img.jpg');
   image.contrast(.4)
   .write('/home/jimp/contrast.jpg');
}

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

輸出

使用 Node JIMP – 帶有 'cb' 引數的 Contrast()

示例

const Jimp = require('jimp') ;

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

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

輸出

更新於: 2021年4月27日

707 次檢視

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.