Open In App

Node.js GM threshold() Function

The threshold() function is an inbuilt function in the GraphicsMagick library which is used to modify the image such that any pixel’s intensity value greater than the threshold is assigned the maximum intensity (white), or otherwise is assigned the minimum intensity (black). The function returns the true value of success.

Syntax: 



threshold( value, flag ) 

Parameters: This function accepts two parameters as mentioned above and described below: 

Return Value: This function returns the GraphicsMagick object.



Original Image: 
 


Example 1: 




// Include gm library
const gm = require('gm');
 
// Import the image
 
// Invoke threshold function
.threshold(30, true)
 
// Process and Write the image
.write("threshold1.png", function (err) {
    if (!err) console.log('done');
});

Output: 

Example 2: Overwriting the file. 




// Include gm library
const gm = require('gm');
 
//Import the image
gm(600, 300, 'white')
 
// set the color for the stroke
.stroke("green", 3)
 
// Set the font
.font("Helvetica.ttf", 60)
 
//Call to drawText Function
.drawText(100, 280, "GeeksforGeeks!")
 
// Invoke threshold function
.threshold(30, false)
 
// Process and write the image
.write("threshold2.png", function (err) {
    if (!err) console.log('done');
});

Output: 

Reference: 


Article Tags :