Skip to main content

Command Palette

Search for a command to run...

Image processing in node.js

Published
1 min read
Image processing in node.js

Hey there, In this article, we would be working on, that how you can perform various tasks such as crop, rotate, applying filters and etc. on an image.

This article would be really short and sweet, as sharp (the library we are using) is powerful and great. It does all the things under the hood, so we need to worry.

Setup

Firstly, we would set up our project by installing one dependency i.e sharp. Sharp is a great library for image processing. So, install the sharp by running npm install sharp.

const sharp = require('sharp');

Great, now let's code.

Converting images

async function convertImg(img) {
    return await sharp(img)
          .toFile('output.png')
}   

convertImg('/images/input.jpeg')

Resizing quality of images

async function resizeBy(img, { height, width }) {
    return await sharp(img)
            .resize({ height, width })
            .toFile('output.png')
}

Rotating images

async function rotateImage(img) {
    return await sharp(img)
                .rotate()
                .resize(null, 200)
                .toFile('output.png')
}

and you can even do tons & tons more manipulation with sharp.

More from this blog

P

Piyush Garg

22 posts

22 years old application and web developer. Certified in C, C++, And Java. Completed IT Trainings in Web Development, Javascript, MERN and web Designs, Android.