Open In App

Node Jimp | opacity

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The opacity() function is an inbuilt function in Nodejs | Jimp which multiplies the opacity of each pixel by a factor between 0 and 1.

Syntax: 

opacity(f, cb)

Parameter: 

  • f: This parameter stores the value by which the image is made opaque. It has a range of 0 to 1. 1 will turn the image completely transparent.
  • cb: This is an optional parameter that is invoked when compilation is complete.

Input Images: 

 


Setup Environment: 

npm init -y

Install Dependencies: 

npm install jimp

Example 1: In this example, we will see the use of opacity().

javascript




// npm install --save jimp
// import jimp library to the environment
const Jimp = require('jimp');
 
// User-Defined Function to read the images
async function main() {
    const image = await Jimp.read(
    // opacity function
    image.opacity(.3)
        .write('opacity1.png');
}
 
main();
console.log("Image Processing Completed");


Output: 

javascript




// npm install --save jimp
// import jimp library to the environment
const Jimp = require('jimp');
 
// User-Defined Function to read the images
async function main() {
    const image = await Jimp.read(
    // opacity function using callback function
    image.opacity(.5, function (err) {
        if (err) throw err;
    })
        .write('opacity2.png');
}
 
main();
console.log("Image Processing Completed");


Output: 


Reference: https://www.npmjs.com/package/jimp



Last Updated : 27 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads