Convert Figma logo to code with AI

lovell logosharp

High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP, AVIF and TIFF images. Uses the libvips library.

28,864
1,290
28,864
107

Top Related Projects

9,518

A fast image processing library with low memory needs.

Node canvas is a Cairo backed Canvas implementation for NodeJS.

13,761

An image processing library written entirely in JavaScript for Node, with zero external or native dependencies.

6,946

GraphicsMagick for node

Content aware image cropping

3,739

Resize image in browser with high quality and high speed

Quick Overview

Sharp is a high-performance Node.js image processing library. It leverages the libvips image processing library to provide fast and efficient operations on common image formats. Sharp is designed to be memory-efficient and suitable for processing large images and high volumes of images.

Pros

  • Extremely fast performance compared to other Node.js image processing libraries
  • Low memory footprint, suitable for processing large images
  • Supports a wide range of image formats and operations
  • Well-documented API with TypeScript support

Cons

  • Requires compilation of native dependencies, which can be challenging in some environments
  • Learning curve may be steeper compared to simpler image processing libraries
  • Limited support for advanced image manipulation techniques (e.g., layers, masks)
  • Dependency on libvips may introduce compatibility issues in certain scenarios

Code Examples

Resizing an image:

import sharp from 'sharp';

sharp('input.jpg')
  .resize(300, 200)
  .toFile('output.jpg')
  .then(() => console.log('Image resized successfully'))
  .catch(err => console.error('Error resizing image:', err));

Applying multiple operations:

import sharp from 'sharp';

sharp('input.png')
  .rotate(90)
  .grayscale()
  .blur(0.5)
  .toBuffer()
  .then(data => {
    // Process the resulting buffer
    console.log('Image processed successfully');
  })
  .catch(err => console.error('Error processing image:', err));

Creating a composite image:

import sharp from 'sharp';

sharp('background.png')
  .composite([
    { input: 'overlay.png', top: 10, left: 10 },
    { input: 'text.png', top: 100, left: 100 }
  ])
  .toFile('output.png')
  .then(() => console.log('Composite image created successfully'))
  .catch(err => console.error('Error creating composite image:', err));

Getting Started

To use Sharp in your Node.js project, first install it via npm:

npm install sharp

Then, you can import and use Sharp in your JavaScript or TypeScript files:

import sharp from 'sharp';

sharp('input.jpg')
  .resize(500)
  .toFile('output.jpg')
  .then(() => console.log('Image processing complete'))
  .catch(err => console.error('Error processing image:', err));

This example resizes an input image to a width of 500 pixels (maintaining aspect ratio) and saves it as a new file.

Competitor Comparisons

9,518

A fast image processing library with low memory needs.

Pros of libvips

  • More comprehensive and flexible image processing library
  • Supports a wider range of image formats and operations
  • Can be used directly in C/C++ projects without additional wrappers

Cons of libvips

  • Steeper learning curve due to its extensive API
  • Requires more manual configuration and setup
  • May have a larger footprint in terms of dependencies and installation size

Code Comparison

libvips (C):

VipsImage *in, *out;
vips_image_new_from_file("input.jpg", &in, NULL);
vips_resize(in, &out, 0.5, NULL);
vips_image_write_to_file(out, "output.jpg", NULL);

sharp (JavaScript):

sharp('input.jpg')
  .resize({ width: 300, height: 200 })
  .toFile('output.jpg');

Summary

libvips is a powerful, low-level image processing library with extensive capabilities, while sharp is a high-level Node.js wrapper around libvips that simplifies image processing tasks. libvips offers more flexibility and control but requires more expertise, whereas sharp provides a more user-friendly interface for common image manipulation tasks in Node.js applications.

Node canvas is a Cairo backed Canvas implementation for NodeJS.

Pros of node-canvas

  • More versatile for general-purpose canvas operations and drawing
  • Supports a wider range of canvas-based tasks, including text rendering and SVG parsing
  • Closer to the HTML5 Canvas API, making it familiar for web developers

Cons of node-canvas

  • Generally slower performance for image processing tasks
  • Heavier dependency footprint, requiring more system libraries
  • More complex installation process, especially on Windows

Code Comparison

node-canvas:

const { createCanvas } = require('canvas');
const canvas = createCanvas(200, 200);
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'red';
ctx.fillRect(0, 0, 100, 100);

sharp:

const sharp = require('sharp');
sharp('input.jpg')
  .resize(200, 200)
  .extract({ left: 0, top: 0, width: 100, height: 100 })
  .toFile('output.jpg');

Summary

node-canvas is better suited for complex drawing tasks and scenarios requiring Canvas API compatibility. sharp excels in high-performance image processing and resizing operations. Choose node-canvas for flexibility in canvas operations, and sharp for efficient image manipulations and transformations.

13,761

An image processing library written entirely in JavaScript for Node, with zero external or native dependencies.

Pros of Jimp

  • Pure JavaScript implementation, making it easier to install and use in various environments
  • Supports a wide range of image manipulations without external dependencies
  • Lightweight and suitable for browser-based applications

Cons of Jimp

  • Generally slower performance compared to Sharp, especially for large images
  • Limited support for advanced image formats and operations
  • May consume more memory for complex operations

Code Comparison

Sharp:

sharp(inputBuffer)
  .resize(300, 200)
  .toFormat('webp')
  .toBuffer((err, buffer) => {
    // Handle result
  });

Jimp:

Jimp.read(inputBuffer)
  .then(image => {
    return image.resize(300, 200).getBufferAsync(Jimp.MIME_WEBP);
  })
  .then(buffer => {
    // Handle result
  });

Both Sharp and Jimp are popular image processing libraries for Node.js, but they cater to different use cases. Sharp is known for its high performance and extensive feature set, leveraging native libraries for speed. It's ideal for server-side applications handling large volumes of images. Jimp, on the other hand, offers a pure JavaScript solution that's more portable and easier to set up, making it suitable for simpler image processing tasks and browser-based applications. The choice between the two depends on specific project requirements, performance needs, and deployment environment.

6,946

GraphicsMagick for node

Pros of gm

  • Supports a wider range of image formats
  • More extensive API with additional image manipulation options
  • Better compatibility with older versions of ImageMagick

Cons of gm

  • Slower performance compared to sharp
  • Larger installation size due to GraphicsMagick dependency
  • Less active development and maintenance

Code Comparison

sharp:

sharp(input)
  .resize(300, 200)
  .toFile('output.jpg', (err, info) => {
    // Handle result
  });

gm:

gm(input)
  .resize(300, 200)
  .write('output.jpg', (err) => {
    // Handle result
  });

Summary

sharp and gm are both popular Node.js image processing libraries. sharp focuses on performance and modern features, while gm offers broader format support and a more extensive API. sharp is generally faster and has a smaller footprint, making it ideal for high-performance applications. gm, on the other hand, provides more flexibility in terms of supported formats and manipulation options, but at the cost of speed and installation size. The choice between the two depends on specific project requirements, such as performance needs, supported formats, and desired image manipulation capabilities.

Content aware image cropping

Pros of smartcrop.js

  • Pure JavaScript implementation, making it easy to use in browser environments
  • Focuses specifically on content-aware image cropping
  • Lightweight and has minimal dependencies

Cons of smartcrop.js

  • Limited to image cropping functionality
  • May be slower for large-scale image processing tasks
  • Lacks advanced image manipulation features

Code Comparison

smartcrop.js:

smartcrop.crop(image, { width: 100, height: 100 }).then(function(result) {
  console.log(result);
});

sharp:

sharp(inputBuffer)
  .resize(100, 100, { fit: 'cover' })
  .toBuffer((err, buffer) => {
    if (err) return console.error(err);
    console.log(buffer);
  });

Summary

smartcrop.js is a specialized library for content-aware image cropping, ideal for browser-based applications. It's lightweight and easy to use but limited in scope. sharp, on the other hand, is a comprehensive image processing library with a wide range of features, including resizing, format conversion, and various manipulations. While sharp offers more functionality and better performance for large-scale tasks, it's primarily designed for server-side use and has more dependencies.

3,739

Resize image in browser with high quality and high speed

Pros of pica

  • Pure JavaScript implementation, making it easier to use in browser environments
  • Supports resizing images with high-quality lanczos filter
  • Lighter weight and simpler to integrate for basic image resizing needs

Cons of pica

  • Limited functionality compared to sharp (focuses mainly on resizing)
  • May be slower for large-scale image processing tasks
  • Lacks advanced features like image composition, color manipulation, and format conversion

Code Comparison

pica:

const pica = require('pica')();

pica.resize(sourceCanvas, destinationCanvas)
  .then(result => console.log('Resized'));

sharp:

const sharp = require('sharp');

sharp('input.jpg')
  .resize(300, 200)
  .toFile('output.jpg')
  .then(() => console.log('Resized'));

Summary

pica is a lightweight, browser-friendly image resizing library, while sharp is a more comprehensive image processing toolkit. pica is ideal for simple resizing tasks, especially in browser environments, but sharp offers more advanced features and better performance for server-side image processing. The choice between them depends on the specific requirements of your project and the environment in which it will be used.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

sharp

sharp logo

The typical use case for this high speed Node-API module is to convert large images in common formats to smaller, web-friendly JPEG, PNG, WebP, GIF and AVIF images of varying dimensions.

It can be used with all JavaScript runtimes that provide support for Node-API v9, including Node.js (^18.17.0 or >= 20.3.0), Deno and Bun.

Resizing an image is typically 4x-5x faster than using the quickest ImageMagick and GraphicsMagick settings due to its use of libvips.

Colour spaces, embedded ICC profiles and alpha transparency channels are all handled correctly. Lanczos resampling ensures quality is not sacrificed for speed.

As well as image resizing, operations such as rotation, extraction, compositing and gamma correction are available.

Most modern macOS, Windows and Linux systems do not require any additional install or runtime dependencies.

Documentation

Visit sharp.pixelplumbing.com for complete installation instructions, API documentation, benchmark tests and changelog.

Examples

npm install sharp
const sharp = require('sharp');

Callback

sharp(inputBuffer)
  .resize(320, 240)
  .toFile('output.webp', (err, info) => { ... });

Promise

sharp('input.jpg')
  .rotate()
  .resize(200)
  .jpeg({ mozjpeg: true })
  .toBuffer()
  .then( data => { ... })
  .catch( err => { ... });

Async/await

const semiTransparentRedPng = await sharp({
  create: {
    width: 48,
    height: 48,
    channels: 4,
    background: { r: 255, g: 0, b: 0, alpha: 0.5 }
  }
})
  .png()
  .toBuffer();

Stream

const roundedCorners = Buffer.from(
  '<svg><rect x="0" y="0" width="200" height="200" rx="50" ry="50"/></svg>'
);

const roundedCornerResizer =
  sharp()
    .resize(200, 200)
    .composite([{
      input: roundedCorners,
      blend: 'dest-in'
    }])
    .png();

readableStream
  .pipe(roundedCornerResizer)
  .pipe(writableStream);

Contributing

A guide for contributors covers reporting bugs, requesting features and submitting code changes.

Licensing

Copyright 2013 Lovell Fuller and others.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

NPM DownloadsLast 30 Days