Resize Images

Resize image using jimp module of node.js

30 Days of Node | Node.js Tutorial series



Overview

In this article we will learn how we can resize an image in node.js using jimp package.

Installation

We can install the jimp package using the following command:

								
>npm install jimp												
								
							

Image Resizing

Suppose we want to resize yellow.png which is in the same directory as our code. We can perform it in the following way :

								
//Name of the File : image-resize-jimp.js
var jimp = require("jimp");
 
// open a file called "yellow.png"
jimp.read("yellow.png", function (err, img) {
    if (err) throw err;
    img.resize(500, 500)            // resize
         .quality(70)                 // set JPEG quality
         .greyscale()                 // set greyscale
         .write("yellow-med.jpg"); // save
          console.log('Resized !!')
});											
								
							

Run

We can run the code using the following command and we will get the output shown below:

								
>node image-resize-jimp.js	
Resized !!										
								
							


The resized output image will be stored in the same directory as the input image.

Summary

We have learned how we can resize an image in node.js using jimp.