Quick and Fast Image Processing Module in Node.js
Sharp is an image processing module which uses the libvips library to provide fast processing for Node.js projects.
Sharp is a fast processing image module for Node.js projects that are powered by libvips image processing library. Due to this addition, Sharp like the name suggests has a high speed processing power as it only stores a few regions of the uncompressed image data in the memory freeing up much of the used CPU power and L1/L2/L3 cache
The library also makes sure that no child processes are spawned and async or await processes are supported. As an open source image processing module, Sharp supports multiple image formats and uncompressed raw pixel data as well. For optimal performance make sure you are using Node.js version 10 and above.
Getting Started with Sharp
The recommended way to install Sharp via NPM. Please use the following command to install it.
Install Sharp via NPM
npm install sharp
Resize Images via Free JavaScript API
The open source sharp library allows JavaScript developers to resize images programmatically. Sharp allows you to convert large image formats into smaller sizes that are compatible with different web applications. With the help of the sharp library, Sharp has a 4 to 5 times faster image processing speed as compared to other image format converters such as ImageMagick or GraphicsMagick. Sharp ensures that the image quality isn’t compromised by Lanczos resampling module.
Read & Resize Images via JavaScript
const transformer = sharp()
.resize({
width: 200,
height: 200,
fit: sharp.fit.cover,
position: sharp.strategy.entropy
});
// Read image data from readableStream
// Write 200px square auto-cropped image data to writableStream
readableStream
.pipe(transformer)
.pipe(writableStream)
Image Processing via Free JavaScript API
The sharp library enables software developers to process the image in the JavaScript framework. You can perform image operations such as image resizing, rotating, extracting, corrections and compositing. Sharp is compatible with MacOS, Windows, and Linux systems and doesn’t require any additional installations when using the Node.js Version 10 and above.
Extract Image from a Region via JavaScript
sharp(input)
.extract({ left: leftOffsetPre, top: topOffsetPre, width: widthPre, height: heightPre })
.resize(width, height)
.extract({ left: leftOffsetPost, top: topOffsetPost, width: widthPost, height: heightPost })
.toFile(output, function(err) {
// Extract a region, resize, then extract from the resized image
});
Rotate Image via JavaScript API
const rotateThenResize = await sharp(input)
.rotate(90)
.resize({ width: 16, height: 8, fit: 'fill' })
.toBuffer();
const resizeThenRotate = await sharp(input)
.resize({ width: 16, height: 8, fit: 'fill' })
.rotate(90)
.toBuffer();
Work with Image Metadata in JavaScript Apps
The open source sharp library has included full support for working with Image Metadata inside JavaScript applications. The library provides speedy access to image metadata without decoding any compressed pixel data. It has included support for several important properties such as image size, format, height, width, depth, density, space, levels, background, exif, hasprofile, page, and many more. You can also easily access pixel-derived image statistics for every channel in the image such as min, max, sum, mean, sharpness, entropy, and many more.
Access Image Metadata via JavaScript
const image = sharp(inputJpg);
image
.metadata()
.then(function(metadata) {
return image
.resize(Math.round(metadata.width / 2))
.webp()
.toBuffer();
})
.then(function(data) {
// data contains a WebP image half the width and height of the original JPEG
});