Open In App

Node Jimp | pixelate

The pixelate() function is an inbuilt function in Nodejs | Jimp that applies the pixelation effect over an image or region. 

Syntax: 



pixelate(size, x, y, w, h, cb)

Parameter: 

Input Images: 



Setup Environment : 

npm init -y

Install Dependencies : 

npm install jimp

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




//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('../gfg.png');
    // pixelate function
    image.pixelate(5)
        .write('pixelate1.png');
}
 
main();
console.log("Image Processing Completed");

Output: 


Example 2: With size, x, y, w, h, and cb (optional parameters) 




//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('../gfg1.png');
    //pixelate function using callback function
    image.pixelate(5, 40, 50, 190, 200, function (err) {
        if (err) throw err;
    })
        .write('pixelate2.png');
}
 
main();
console.log("Image Processing Completed");

Output: 


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


Article Tags :