Which is Better Image Processing Libraries?
image-size vs sharp vs canvas vs jimp vs gm
1 Year
image-sizesharpcanvasjimpgmSimilar Packages:
What's Image Processing Libraries?

Image processing libraries in Node.js provide developers with tools to manipulate and analyze images programmatically. These libraries facilitate tasks such as resizing, cropping, filtering, and format conversion, enabling dynamic image generation and optimization for web applications. They are essential for applications that require image manipulation on the server side, such as content management systems, e-commerce platforms, and social media applications. Each library has its unique strengths, making it crucial to choose the right one based on project requirements and performance considerations.

NPM Package Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
image-size10,715,6722,04650 kB3310 months agoMIT
sharp6,843,25129,245503 kB1153 months agoApache-2.0
canvas2,432,93510,182518 kB4602 years agoMIT
jimp1,283,099-4.03 MB-2 months agoMIT
gm339,6506,955121 kB367-MIT
Feature Comparison: image-size vs sharp vs canvas vs jimp vs gm

Performance

  • image-size: Image-size is extremely lightweight and performs exceptionally well for its intended purpose of extracting dimensions, ensuring minimal overhead and fast execution times.
  • sharp: Sharp is one of the fastest image processing libraries available in Node.js, leveraging libvips for efficient memory usage and speed. It is well-suited for production environments where performance is critical.
  • canvas: Canvas provides a flexible drawing API but may not be the fastest option for heavy image processing tasks due to its reliance on the CPU for rendering operations. Performance can vary based on the complexity of the graphics being drawn.
  • jimp: Jimp is easy to use but may not be the fastest option for large images or complex operations, as it is a pure JavaScript implementation. Performance can degrade with larger images or more intensive processing tasks.
  • gm: GraphicsMagick is optimized for batch processing and can handle multiple images simultaneously, making it efficient for tasks that require processing large sets of images. However, it may not be as fast as sharp for single-image operations.

Ease of Use

  • image-size: Image-size is extremely easy to use, providing a simple interface for retrieving image dimensions and metadata with minimal setup required.
  • sharp: Sharp has a well-documented API that balances performance with usability, making it relatively easy to integrate into applications while still offering advanced features.
  • canvas: Canvas has a steeper learning curve due to its extensive API for drawing and rendering, making it more suitable for developers familiar with graphics programming.
  • jimp: Jimp is designed for simplicity and ease of use, with a clear and intuitive API that allows developers to perform basic image manipulations quickly and efficiently.
  • gm: GM offers a straightforward API that abstracts many complexities of image processing, making it accessible for developers who need to perform common tasks without deep knowledge of image manipulation.

Supported Formats

  • image-size: Image-size is limited to extracting dimensions and metadata, but it can work with various formats as long as they are supported by the underlying libraries.
  • sharp: Sharp supports a wide variety of image formats, including PNG, JPEG, WebP, and TIFF, making it suitable for applications that require flexibility in image handling.
  • canvas: Canvas supports a wide range of image formats for input and output, including PNG, JPEG, and GIF, making it versatile for various applications.
  • jimp: Jimp supports common formats like PNG and JPEG, but may not handle as many formats as other libraries, which can limit its use in some scenarios.
  • gm: GraphicsMagick supports an extensive list of image formats, including less common ones, making it a good choice for applications that need to handle diverse image types.

Dependency Management

  • image-size: Image-size is a pure JavaScript library with no native dependencies, making it easy to install and use across different environments without additional configuration.
  • sharp: Sharp has native dependencies but provides pre-built binaries for various platforms, simplifying installation while still requiring some system-level configuration.
  • canvas: Canvas requires native dependencies and may involve additional setup steps, which can complicate installation in some environments, especially on Windows.
  • jimp: Jimp is also a pure JavaScript library, ensuring easy installation and compatibility across platforms without the need for native binaries.
  • gm: GM is a wrapper around GraphicsMagick, which may require installation of the underlying GraphicsMagick library, adding complexity to the setup process.

Image Manipulation Capabilities

  • image-size: Image-size is limited to retrieving dimensions and metadata, lacking manipulation capabilities, which makes it unsuitable for tasks requiring image editing.
  • sharp: Sharp provides extensive image manipulation capabilities, including resizing, rotation, and format conversion, with a focus on performance and efficiency, making it ideal for high-demand applications.
  • canvas: Canvas excels in custom drawing and rendering, allowing for complex graphics and animations, making it suitable for applications that require artistic rendering.
  • jimp: Jimp offers a good range of basic image manipulation functions, such as resizing, cropping, and filtering, making it suitable for simple tasks but not for advanced processing.
  • gm: GM provides a wide range of image manipulation capabilities, including resizing, cropping, and format conversion, making it versatile for many image processing tasks.
How to Choose: image-size vs sharp vs canvas vs jimp vs gm
  • image-size: Choose image-size if you only need to extract dimensions and metadata from images without any manipulation. It is lightweight and efficient for quickly obtaining image sizes, making it suitable for applications that require minimal overhead.
  • sharp: Choose sharp for high-performance image processing, especially when dealing with large images or requiring fast transformations. It is built on libvips, making it extremely efficient in terms of speed and memory usage, ideal for production environments.
  • canvas: Choose canvas if you need a powerful 2D drawing API that closely resembles the HTML5 canvas element, allowing for complex graphics and image manipulation. It is ideal for applications that require custom rendering and drawing operations.
  • jimp: Choose jimp for a pure JavaScript solution that is easy to use and does not require native dependencies. It is great for simple image manipulations and is particularly useful for projects that prioritize ease of installation and cross-platform compatibility.
  • gm: Choose gm (GraphicsMagick) if you need a simple interface for image processing with a wide range of supported formats and operations. It is particularly useful for batch processing images and executing complex transformations without deep knowledge of image processing.
README for image-size

image-size

Build Status Package Version Downloads

A Node module to get dimensions of any image file

Supported formats

  • BMP
  • CUR
  • DDS
  • GIF
  • HEIC (HEIF, AVCI, AVIF)
  • ICNS
  • ICO
  • J2C
  • JP2
  • JPEG
  • KTX (1 and 2)
  • PNG
  • PNM (PAM, PBM, PFM, PGM, PPM)
  • PSD
  • SVG
  • TGA
  • TIFF
  • WebP

Programmatic Usage

npm install image-size --save

or

yarn add image-size

Synchronous

const sizeOf = require("image-size")
const dimensions = sizeOf("images/funny-cats.png")
console.log(dimensions.width, dimensions.height)

Asynchronous

const sizeOf = require("image-size")
sizeOf("images/funny-cats.png", function (err, dimensions) {
  console.log(dimensions.width, dimensions.height)
})

NOTE: The asynchronous version doesn't work if the input is a Buffer. Use synchronous version instead.

Also, the asynchronous functions have a default concurrency limit of 100 To change this limit, you can call the setConcurrency function like this:

const sizeOf = require("image-size")
sizeOf.setConcurrency(123456)

Using promises (nodejs 10.x+)

const { promisify } = require("util")
const sizeOf = promisify(require("image-size"))
sizeOf("images/funny-cats.png")
  .then((dimensions) => {
    console.log(dimensions.width, dimensions.height)
  })
  .catch((err) => console.error(err))

Async/Await (Typescript & ES7)

const { promisify } = require("util")
const sizeOf = promisify(require("image-size"))(async () => {
  try {
    const dimensions = await sizeOf("images/funny-cats.png")
    console.log(dimensions.width, dimensions.height)
  } catch (err) {
    console.error(err)
  }
})().then((c) => console.log(c))

Multi-size

If the target file is an icon (.ico) or a cursor (.cur), the width and height will be the ones of the first found image.

An additional images array is available and returns the dimensions of all the available images

const sizeOf = require("image-size")
const images = sizeOf("images/multi-size.ico").images
for (const dimensions of images) {
  console.log(dimensions.width, dimensions.height)
}

Using a URL

const url = require("url")
const http = require("http")

const sizeOf = require("image-size")

const imgUrl = "http://my-amazing-website.com/image.jpeg"
const options = url.parse(imgUrl)

http.get(options, function (response) {
  const chunks = []
  response
    .on("data", function (chunk) {
      chunks.push(chunk)
    })
    .on("end", function () {
      const buffer = Buffer.concat(chunks)
      console.log(sizeOf(buffer))
    })
})

You can optionally check the buffer lengths & stop downloading the image after a few kilobytes. You don't need to download the entire image

Disabling certain image types

const imageSize = require("image-size")
imageSize.disableTypes(["tiff", "ico"])

Disabling all file-system reads

const imageSize = require("image-size")
imageSize.disableFS(true)

JPEG image orientation

If the orientation is present in the JPEG EXIF metadata, it will be returned by the function. The orientation value is a number between 1 and 8 representing a type of orientation.

const sizeOf = require("image-size")
const dimensions = sizeOf("images/photo.jpeg")
console.log(dimensions.orientation)

Command-Line Usage (CLI)

npm install image-size --global

or

yarn global add image-size

followed by

image-size image1 [image2] [image3] ...

Credits

not a direct port, but an attempt to have something like dabble's imagesize as a node module.

Contributors