Image Processing with NodeJS JIMP

Node.jsServer Side ProgrammingProgramming

JIMP, also known as JavaScript Image Manipulation Program, is an image processing library for Node written in JavaScript with no other dependency. It allows the user to easily manipulate the and transform the images into any required shape, format, dimnesion or style. It can also be used for optimizing images to minimize the file size, ensuring high visualquality or reducing the bandwidth.

Using JIMP, you can resize and crop images, convert them to the format as needed and also apply different filters and affects. Following are image formats that are supported by NodeJS JIMP −

  • @jimp/jpeg

  • @jimp/png

  • @jimp/bmp

  • @jimp/tiff

  • @jimp/gif

Installation

Setup Environment -

npm init -y

Installing Dependecy -

npm install --save jimp

Using NodeJS Jimp

Jimp extends two functionalities – Callback and Promise API's for manipulating images. Here, we will use JIMP's Promise API.

The static Jimp.read method takes an image as an input. The input could either be a location of an Image file in the file system, a web address (URL), Jimp instance or a stream buffer.After processing it returns a promise.

Example

// npm install --save jimp for importing jimp
const Jimp = require('jimp') ;
async function main() {
   // Reading Image
   const image = await Jimp. read
   ('/home/abc/tutorials_point.jpg');
   image.blur(2, function(err){
      if (err) throw err;
   })
   .write('/home/abc/tutorials_point-BLUR.jpg');
}

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

Input - Original Image

Output – Blurred Image (blur2.jpeg)


raja
Published on 27-Apr-2021 11:17:12
Advertisements