Jimp
The "JavaScript Image Manipulation Program" :-)
An image processing library for Node written entirely in JavaScript, with zero external or native dependencies.
Example usage:
var Jimp = require("jimp");
var lenna = new Jimp("lenna.png", function (err, image) {
this.resize(512, 512)
.write("lenna-small.png")
.quality(60)
.write("lenna-small.jpg")
.greyscale()
.write("lena-small-bw.png")
.crop(128, 192, 256, 128)
.write("lena-small-bw-cropped.png");
});
Basic usage
The Jimp constructor takes two arguments, the path to a PNG, JPEG or BMP image and a Node-style callback:
var image = new Jimp("./path/to/image.jpg", function (err, image) {
});
Once the callback has fired, the following methods can be called on the image:
image.crop( x, y, w, h );
image.invert();
image.flip( horz, vert);
image.gaussian( r );
image.blur( r );
image.greyscale();
image.sepia();
image.opacity( f );
image.resize( w, h );
image.scale( f );
image.rotate( deg[, resize] );
image.blit( src, x, y );
image.composite( src, x, y );
image.brightness( val );
image.contrast( val );
image.posterize( n );
image.mask( src, x, y );
image.dither565();
image.cover( w, h );
image.contain( w, h );
image.background( hex );
image.mirror( horz, vert);
image.fade( f );
(Contributions of more methods are welcome!)
The image can be written to disk in PNG, JPEG or BMP format (determined by the file extension) using:
image.write( path, cb );
The quality of saved JPEGs can be set with:
image.quality( n );
The format of saved PNGs can be set with:
image.rgba( bool );
Cloning images
To clone a Jimp image, you can use:
image.clone();
The Jimp constructor can also be called using an existing image create a clone of that image:
var clone = new Jimp(image, function (err, clone) {
});
Working with Buffers
A PNG, JPEG or BMP binary Buffer of an image (e.g. for storage in a database) can to got using:
image.getBuffer( mime, cb );
For convenience, supported MIME types are available as static properties:
Jimp.MIME_PNG;
Jimp.MIME_JPEG;
Jimp.BMP;
The Jimp constructor can also be called passing a valid Buffer as the first argument to the Jimp constructor:
var image = new Jimp(buffer, function (err, image) {
});
Direct manipulation
Jimp enables low-level manipulation of images in memory through the bitmap property of each Jimp object:
image.bitmap.data;
image.bitmap.width;
image.bitmap.height
This data can be manipulated directly but remember: garbage in, garbage out.
A helper method is available to scan a region of the bitmap:
image.scan(x, y, w, h, cb);
Example usage:
image.scan(0, 0, image.bitmap.width, image.bitmap.height, function (x, y, idx) {
var red = this.bitmap.data[idx];
var green = this.bitmap.data[idx+1];
var blue = this.bitmap.data[idx+2];
var alpha = this.bitmap.data[idx+3];
});
Alternatively, you can manipulate individual pixels using the following these functions:
image.getPixelColor(x, y);
image.setPixelColor(hex, x, y);
Two static helper functions exist to convert RGBA values into single integer (hex) values:
Jimp.rgbaToInt(r, g, b, a);
Jimp.intToRGBA(hex);
If you want to begin with an empty Jimp image, you can call the Jimp constructor passing the width and height of the image to create:
var image = new Jimp(256, 256, function (err, image) {
});
You can optionally set the pixel colour as follows:
var image = new Jimp(256, 256, 0xFF0000FF, function (err, image) {
});
Chaining or callbacks
All methods can be chained together, for example as follows:
var lenna = new Jimp("lenna.png", function (err, image) {
this.greyscale().scale(0.5).write("lena-half-bw.png");
});
Alternatively, methods can be passed Node-style callbacks:
var lenna = new Jimp("lenna.png", function (err, image) {
image.greyscale(function(err, image) {
image.scale(0.5, function (err, image) {
image.write("lena-half-bw.png");
});
});
});
The Node-style callback pattern allows Jimp to be used with frameworks that expect or build on the Node-style callback pattern.
License
Jimp is licensed under the MIT license.