The typical use case for this high speed Node.js module is to convert large images of many formats to smaller, web-friendly JPEG, PNG and WebP images of varying dimensions.
This module supports reading and writing JPEG, PNG and WebP images to and from Streams, Buffer objects and the filesystem. It also supports reading images of many other types from the filesystem via libmagick++ or libgraphicsmagick++ if present. Colour spaces, embedded ICC profiles and alpha transparency channels are all handled correctly.
Only small regions of uncompressed image data are held in memory and processed at a time, taking full advantage of multiple CPU cores and L1/L2/L3 cache. Resizing an image is typically 4x faster than using the quickest ImageMagick and GraphicsMagick settings.
Huffman tables are optimised when generating JPEG output images without having to use separate command line tools like jpegoptim and jpegtran. PNG filtering can be disabled, which for diagrams and line art often produces the same result as pngcrush.
Everything remains non-blocking thanks to libuv, no child processes are spawned and Promises/A+ are supported.
Anyone who has used the Node.js bindings for GraphicsMagick will find the API similarly fluent.
This module is powered by the blazingly fast libvips image processing library, originally created in 1989 at Birkbeck College and currently maintained by John Cupitt.
npm install sharp
- Node.js v0.10+
- libvips v7.40.0+ (7.42.0+ recommended)
To install the most suitable version of libvips on the following Operating Systems:
- Mac OS
- Homebrew
- MacPorts
- Debian Linux
- Debian 7, 8
- Ubuntu 12.04, 14.04, 14.10, 15.04
- Mint 13, 17
- Red Hat Linux
- RHEL/Centos/Scientific 6, 7
- Fedora 21, 22
- Amazon Linux 2014.09
run the following as a user with sudo
access:
curl -s https://raw.githubusercontent.com/lovell/sharp/master/preinstall.sh | sudo bash -
or run the following as root
:
curl -s https://raw.githubusercontent.com/lovell/sharp/master/preinstall.sh | bash -
The preinstall.sh script requires curl
and pkg-config
.
Manual install via homebrew:
brew install homebrew/science/vips --with-webp --with-graphicsmagick
A missing or incorrectly configured Xcode Command Line Tools installation can lead to a library not found for -ljpeg
error. If so, please try:
xcode-select --install
The gettext dependency of libvips can lead to a library not found for -lintl
error. If so, please try:
brew link gettext --force
Alessandro Tagliapietra maintains an Heroku buildpack for libvips and its dependencies.
Marc Bachmann maintains a Dockerfile for libvips.
docker pull marcbachmann/libvips
Eugeny Vlasenko maintains gulp-responsive and Mohammad Prabowo maintains gulp-sharp.
var sharp = require('sharp');
sharp('input.jpg').resize(300, 200).toFile('output.jpg', function(err) {
if (err) {
throw err;
}
// output.jpg is a 300 pixels wide and 200 pixels high image
// containing a scaled and cropped version of input.jpg
});
var transformer = sharp().resize(300, 200).crop(sharp.gravity.north);
readableStream.pipe(transformer).pipe(writableStream);
// Read image data from readableStream, resize and write image data to writableStream
var image = sharp(inputJpg);
image.metadata(function(err, metadata) {
image.resize(metadata.width / 2).webp().toBuffer(function(err, outputBuffer, info) {
// outputBuffer contains a WebP image half the width and height of the original JPEG
});
});
var pipeline = sharp()
.rotate()
.resize(null, 200)
.progressive()
.toBuffer(function(err, outputBuffer, info) {
if (err) {
throw err;
}
// outputBuffer contains 200px high progressive JPEG image data,
// auto-rotated using EXIF Orientation tag
// info.width and info.height contain the dimensions of the resized image
});
readableStream.pipe(pipeline);
sharp('input.png')
.rotate(180)
.resize(300)
.flatten()
.background('#ff6600')
.sharpen()
.withMetadata()
.quality(90)
.webp()
.toBuffer()
.then(function(outputBuffer) {
// outputBuffer contains upside down, 300px wide, alpha channel flattened
// onto orange background, sharpened, with metadata, 90% quality WebP image
// data
});
http.createServer(function(request, response) {
response.writeHead(200, {'Content-Type': 'image/webp'});
sharp('input.jpg').rotate().resize(200).webp().pipe(response);
}).listen(8000);
// Create HTTP server that always returns auto-rotated 'input.jpg',
// resized to 200 pixels wide, in WebP format
sharp(input)
.extract(top, left, width, height)
.toFile(output);
// Extract a region of the input image, saving in the same format.
sharp(input)
.extract(topOffsetPre, leftOffsetPre, widthPre, heightPre)
.resize(width, height)
.extract(topOffsetPost, leftOffsetPost, widthPost, heightPost)
.toFile(output);
// Extract a region, resize, then extract from the resized image
sharp(inputBuffer)
.resize(200, 300)
.interpolateWith(sharp.interpolator.nohalo)
.background('white')
.embed()
.toFile('output.tiff')
.then(function() {
// output.tiff is a 200 pixels wide and 300 pixels high image
// containing a bicubic scaled version, embedded on a white canvas,
// of the image data in inputBuffer
});
sharp('input.gif')
.resize(200, 300)
.background({r: 0, g: 0, b: 0, a: 0})
.embed()
.webp()
.toBuffer(function(err, outputBuffer) {
if (err) {
throw err;
}
// outputBuffer contains WebP image data of a 200 pixels wide and 300 pixels high
// containing a scaled version, embedded on a transparent canvas, of input.gif
});
sharp(inputBuffer)
.resize(200, 200)
.max()
.jpeg()
.toBuffer().then(function(outputBuffer) {
// outputBuffer contains JPEG image data no wider than 200 pixels and no higher
// than 200 pixels regardless of the inputBuffer image dimensions
});
Constructor to which further methods are chained. input
, if present, can be one of:
- Buffer containing JPEG, PNG, WebP or TIFF image data, or
- String containing the filename of an image, with most major formats supported.
The object returned implements the stream.Duplex class.
JPEG, PNG, WebP or TIFF format image data can be streamed into the object when input
is not provided.
JPEG, PNG or WebP format image data can be streamed out from this object.
Fast access to image metadata without decoding any compressed image data.
callback
, if present, gets the arguments (err, metadata)
where metadata
has the attributes:
format
: Name of decoder to be used to decompress image data e.g.jpeg
,png
,webp
(for file-based input additionallytiff
andmagick
)width
: Number of pixels wideheight
: Number of pixels highspace
: Name of colour space interpretation e.g.srgb
,rgb
,scrgb
,cmyk
,lab
,xyz
,b-w
...channels
: Number of bands e.g.3
for sRGB,4
for CMYKhasProfile
: Boolean indicating the presence of an embedded ICC profilehasAlpha
: Boolean indicating the presence of an alpha transparency channelorientation
: Number value of the EXIF Orientation header, if present
A Promises/A+ promise is returned when callback
is not provided.
An advanced setting that switches the libvips access method to VIPS_ACCESS_SEQUENTIAL
. This will reduce memory usage and can improve performance on some systems.
Scale output to width
x height
. By default, the resized image is cropped to the exact size specified.
width
is the Number of pixels wide the resultant image should be. Use null
or undefined
to auto-scale the width to match the height.
height
is the Number of pixels high the resultant image should be. Use null
or undefined
to auto-scale the height to match the width.
Extract a region of the image. Can be used with or without a resize
operation.
top
and left
are the offset, in pixels, from the top-left corner.
width
and height
are the dimensions of the extracted image.
Use extract
before resize
for pre-resize extraction. Use extract
after resize
for post-resize extraction. Use extract
before and after for both.
Crop the resized image to the exact size specified, the default behaviour.
gravity
, if present, is an attribute of the sharp.gravity
Object e.g. sharp.gravity.north
.
Possible values are north
, east
, south
, west
, center
and centre
. The default gravity is center
/centre
.
Preserving aspect ratio, resize the image to the maximum width
or height
specified.
Both width
and height
must be provided via resize
otherwise the behaviour will default to crop
.
Set the background for the embed
and flatten
operations.
rgba
is parsed by the color module to extract values for red, green, blue and alpha.
The alpha value is a float between 0
(transparent) and 1
(opaque).
The default background is {r: 0, g: 0, b: 0, a: 1}
, black without transparency.
Preserving aspect ratio, resize the image to the maximum width
or height
specified then embed on a background of the exact width
and height
specified.
If the background contains an alpha value then WebP and PNG format output images will contain an alpha channel, even when the input image does not.
Merge alpha transparency channel, if any, with background
.
Rotate the output image by either an explicit angle or auto-orient based on the EXIF Orientation
tag.
angle
, if present, is a Number with a value of 0
, 90
, 180
or 270
.
Use this method without angle
to determine the angle from EXIF data. Mirroring is supported and may infer the use of a flip
operation.
Flip the image about the vertical Y axis. This always occurs after rotation, if any.
Flop the image about the horizontal X axis. This always occurs after rotation, if any.
Do not enlarge the output image if the input image width or height are already less than the required dimensions.
This is equivalent to GraphicsMagick's >
geometry option: "change the dimensions of the image only if its width or height exceeds the geometry specification".
When used without parameters, performs a fast, mild blur of the output image. This typically reduces performance by 10%.
When a sigma
is provided, performs a slower, more accurate Gaussian blur. This typically reduces performance by 25%.
sigma
, if present, is a Number between 0.3 and 1000 representing the approximate blur radius in pixels.
When used without parameters, performs a fast, mild sharpen of the output image. This typically reduces performance by 10%.
When a radius
is provided, performs a slower, more accurate sharpen of the L channel in the LAB colour space. Separate control over the level of sharpening in "flat" and "jagged" areas is available. This typically reduces performance by 50%.
radius
, if present, is an integral Number representing the sharpen mask radius in pixels.flat
, if present, is a Number representing the level of sharpening to apply to "flat" areas, defaulting to a value of 1.0.jagged
, if present, is a Number representing the level of sharpening to apply to "jagged" areas, defaulting to a value of 2.0.
Use the given interpolator for image resizing, where interpolator
is an attribute of the sharp.interpolator
Object e.g. sharp.interpolator.bicubic
.
Possible interpolators, in order of performance, are:
nearest
: Use nearest neighbour interpolation, suitable for image enlargement only.bilinear
: Use bilinear interpolation, the default and fastest image reduction interpolation.bicubic
: Use bicubic interpolation, which typically reduces performance by 5%.vertexSplitQuadraticBasisSpline
: Use VSQBS interpolation, which prevents "staircasing" and typically reduces performance by 5%.locallyBoundedBicubic
: Use LBB interpolation, which prevents some "acutance" and typically reduces performance by a factor of 2.nohalo
: Use Nohalo interpolation, which prevents acutance and typically reduces performance by a factor of 3.
Apply a gamma correction by reducing the encoding (darken) pre-resize at a factor of 1/gamma
then increasing the encoding (brighten) post-resize at a factor of gamma
.
gamma
, if present, is a Number betweem 1 and 3. The default value is 2.2
, a suitable approximation for sRGB images.
This can improve the perceived brightness of a resized image in non-linear colour spaces.
JPEG input images will not take advantage of the shrink-on-load performance optimisation when applying a gamma correction.
Convert to 8-bit greyscale; 256 shades of grey.
This is a linear operation. If the input image is in a non-linear colour space such as sRGB, use gamma()
with greyscale()
for the best results.
The output image will still be web-friendly sRGB and contain three (identical) channels.
Use JPEG format for the output image.
Use PNG format for the output image.
Use WebP format for the output image.
The output quality to use for lossy JPEG, WebP and TIFF output formats. The default quality is 80
.
quality
is a Number between 1 and 100.
Use progressive (interlace) scan for JPEG and PNG output. This typically reduces compression performance by 30% but results in an image that can be rendered sooner when decompressed.
Include all metadata (EXIF, XMP, IPTC) from the input image in the output image. This will also convert to and add the latest web-friendly v2 sRGB ICC profile.
The default behaviour is to strip all metadata and convert to the device-independent sRGB colour space.
An advanced setting for the zlib compression level of the lossless PNG output format. The default level is 6
.
compressionLevel
is a Number between 0 and 9.
Requires libvips 7.42.0+
An advanced setting to disable adaptive row filtering for the lossless PNG output format.
filename
is a String containing the filename to write the image data to. The format is inferred from the extension, with JPEG, PNG, WebP and TIFF supported.
callback
, if present, is called with two arguments (err, info)
where:
err
contains an error message, if any.info
contains the output imageformat
,width
andheight
.
A Promises/A+ promise is returned when callback
is not provided.
Write image data to a Buffer, the format of which will match the input image by default. JPEG, PNG and WebP are supported.
callback
, if present, gets three arguments (err, buffer, info)
where:
err
is an error message, if any.buffer
is the output image data.info
contains the output imageformat
,width
andheight
.
A Promises/A+ promise is returned when callback
is not provided.
If memory
or items
are provided, set the limits of libvips' operation cache.
memory
is the maximum memory in MB to use for this cache, with a default value of 100items
is the maximum number of operations to cache, with a default value of 500
This method always returns cache statistics, useful for determining how much working memory is required for a particular task.
var stats = sharp.cache(); // { current: 75, high: 99, memory: 100, items: 500 }
sharp.cache(200); // { current: 75, high: 99, memory: 200, items: 500 }
sharp.cache(50, 200); // { current: 49, high: 99, memory: 50, items: 200}
threads
, if provided, is the Number of threads libvips' should create for image processing. The default value is the number of CPU cores. A value of 0
will reset to this default.
This method always returns the current concurrency.
var threads = sharp.concurrency(); // 4
sharp.concurrency(2); // 2
sharp.concurrency(0); // 4
Provides access to internal task counters.
queue
is the number of tasks this module has queued waiting for libuv to provide a worker thread from its pool.process
is the number of resize tasks currently being processed.
var counters = sharp.counters(); // { queue: 2, process: 4 }
npm test
cd sharp/test/leak
./leak.sh
Requires valgrind:
brew install valgrind
sudo apt-get install -qq valgrind
cd sharp/test/bench
npm install
npm test
Requires both ImageMagick and GraphicsMagick:
brew install imagemagick
brew install graphicsmagick
sudo apt-get install -qq imagemagick graphicsmagick libmagick++-dev
sudo yum install ImageMagick
sudo yum install -y http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
sudo yum install -y --enablerepo=epel GraphicsMagick
- AWS EC2 c3.xlarge
- Ubuntu 14.04
- libvips 7.40.8
- liborc 0.4.22
- imagemagick-native v1.2.2 - Supports Buffers only
- imagemagick v0.1.3 - Supports filesystem only and "has been unmaintained for a long time".
- gm v1.16.0 - Fully featured wrapper around GraphicsMagick.
- sharp v0.6.2 - Caching within libvips disabled to ensure a fair comparison.
Decompress a 2725x2225 JPEG image, resize and crop to 720x480, then compress to JPEG.
Module | Input | Output | Ops/sec | Speed-up |
---|---|---|---|---|
imagemagick-native | buffer | buffer | 1.58 | 1 |
imagemagick | file | file | 6.23 | 3.9 |
gm | buffer | file | 5.32 | 3.4 |
gm | buffer | buffer | 5.32 | 3.4 |
gm | file | file | 5.36 | 3.4 |
gm | file | buffer | 5.36 | 3.4 |
sharp | buffer | file | 22.05 | 14.0 |
sharp | buffer | buffer | 22.14 | 14.0 |
sharp | file | file | 21.79 | 13.8 |
sharp | file | buffer | 21.90 | 13.9 |
sharp | stream | stream | 20.87 | 13.2 |
sharp +promise | file | buffer | 21.89 | 13.9 |
sharp +sharpen | file | buffer | 19.69 | 12.5 |
sharp +progressive | file | buffer | 16.93 | 10.7 |
sharp +sequentialRead | file | buffer | 21.60 | 13.7 |
You can expect greater performance with caching enabled (default) and using 8+ core machines.
This module would never have been possible without the help and code contributions of the following people:
- John Cupitt
- Pierre Inglebert
- Jonathan Ong
- Chanon Sajjamanochai
- Juliano Julio
- Daniel Gasienica
- Julian Walker
- Amit Pitaru
- Brandon Aaron
- Andreas Lind
Thank you!
Copyright 2013, 2014 Lovell Fuller and contributors.
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 http://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.