如何在 Node Jimp 中更改影像的透明度?


NodeJS – Opacity() 是一個內建函式,用於更改影像的透明度。此函式將每個畫素的透明度乘以 0 到 1 之間的因子以生成輸出影像。

語法

opacity(f, cb)

opacity() 引數的定義

  • – 它接受 0 到 1 之間的值作為輸入,這將是使影像不透明的引數。輸入 1 將使影像完全透明。

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

輸入影像

使用 Node JIMP – OPACITY()

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

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

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

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

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

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

示例

const Jimp = require('jimp') ;

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

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

輸出

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

示例

const Jimp = require('jimp') ;

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

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

輸出

更新於: 2021-04-27

609 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.