This module provides comprehensive, fast, and simple image processing and manipulation capabilities.
There are no external runtime dependencies, which means you don't have to install anything else on your system.
This module is in active development. New features are being added.
Read the background for the development of this module.
npm install lwip
Or, clone this repo and cd lwip && npm install.
You can run tests with npm test.
Note: Installation of this module involves compiling native code.
If npm install lwip failes, you probably need to setup your system.
See instructions.
Building on Windows with Visual Studio requires version 2013 or higher.
Typical workflow:
Example (batch operations):
// obtain an image object:
require('lwip').open('image.jpg', function(err, image){
// check err...
// define a batch of manipulations and save to disk as JPEG:
image.batch()
.scale(0.75) // scale to 75%
.rotate(45, 'white') // rotate 45degs clockwise (white fill)
.crop(200, 200) // crop a 200X200 square from center
.blur(5) // Gaussian blur with SD=5
.writeFile('output.jpg', function(err){
// check err...
// done.
});
});
Example (non-batch):
var lwip = require('lwip');
// obtain an image object:
lwip.open('image.jpg', function(err, image){
// check err...
// manipulate image:
image.scale(0.5, function(err, image){
// check err...
// manipulate some more:
image.rotate(45, 'white', function(err, image){
// check err...
// encode to jpeg and get a buffer object:
image.toBuffer('jpg', function(err, buffer){
// check err...
// save buffer to disk / send over network / etc.
});
});
});
});
Decoding (reading):
Encoding (writing):
Other formats may also be supported in the future, but are probably less urgent. Check the issues to see which formats are planned to be supported. Open an issue if you need support for a format which is not already listed.
In LWIP colors are coded as RGBA values (red, green, blue and an alpha channel).
Colors are specified in one of three ways:
Javascript
"black" // {r: 0, g: 0, b: 0, a: 100}
"white" // {r: 255, g: 255, b: 255, a: 100}
"gray" // {r: 128, g: 128, b: 128, a: 100}
"red" // {r: 255, g: 0, b: 0, a: 100}
"green" // {r: 0, g: 255, b: 0, a: 100}
"blue" // {r: 0, g: 0, b: 255, a: 100}
"yellow" // {r: 255, g: 255, b: 0, a: 100}
"cyan" // {r: 0, g: 255, b: 255, a: 100}
"magenta" // {r: 255, g: 0, b: 255, a: 100}
[R, G, B, A] where R, G and B are integers between 0 and
255 and A is an integer between 0 and 100.{r: R, g: G, b: B, a: A} where R, G and B are integers
between 0 and 255 and A is an integer between 0 and 100.Note: The A value (alpha channel) is always optional and defaults to
100 (completely opaque).
All operations are done on an image object. An image object can be obtained
by:
open
method.create method.image.clone method.image.extract method.lwip.open(source, type, callback)
source {String/Buffer}: The path to the image on disk or an image buffer.type {String/Object}: Optional type of the image. If omitted, the type
will be inferred from the file extension. If source is a buffer, type
must be specified. If source is an encoded image buffer, type must be
a string of the image type (i.e. "jpg"). If source is a raw pixels buffer
type must be an object with type.width and type.height properties.callback {Function(err, image)}Note about raw pixels buffers: source may be a buffer of raw pixels. The
buffer may contain pixels of 1-4 channels, where:
In other words, if the image in the buffer has width W and height H, the
size of the buffer can be W*H, 2*W*H, 3*W*H or 4*W*H.
The channel values in the buffer must be stored sequentially. I.e. first all the Red values, then all the Green values, etc.
var lwip = require('lwip');
lwip.open('path/to/image.jpg', function(err, image){
// check 'err'. use 'image'.
// image.resize(...), etc.
});
var fs = require('fs'),
lwip = require('lwip');
fs.readFile('path/to/image.png', function(err, buffer){
// check err
lwip.open(buffer, 'png', function(err, image){
// check 'err'. use 'image'.
// image.resize(...), etc.
});
});
lwip.create(width, height, color, callback)
width {Integer>0}: The width of the new image.height {Integer>0}: The height of the new image.color {String / Array / Object}: Optional Color of the canvas. See
colors specification. Defaults to a transparent
canvas {r:0, g:0, b:0, a:0}.callback {Function(err, image)}Example:
var lwip = require('lwip');
lwip.create(500, 500, 'yellow', function(err, image){
// check err
// 'image' is a 500X500 solid yellow canvas.
});
image.resize(width, height, inter, callback)
width {Integer}: Width in pixels.height {Integer}: Optional height in pixels. If omitted, width will
be used.inter {String}: Optional interpolation method. Defaults to "lanczos".
Possible values:"nearest-neighbor""moving-average""linear""grid""cubic""lanczos"callback {Function(err, image)}image.scale(wRatio, hRatio, inter, callback)
wRatio {Float}: Width scale ratio.hRatio {Float}: Optional height scale ratio. If omitted, wRatio will
be used.inter {String}: Optional interpolation method. Defaults to "lanczos".
Possible values:"nearest-neighbor""moving-average""linear""grid""cubic""lanczos"callback {Function(err, image)}Contain the image in a colored canvas. The image will be resized to the largest possible size such that it's fully contained inside the canvas.
image.contain(width, height, color, inter, callback)
width {Integer}: Canvas' width in pixels.height {Integer}: Canvas' height in pixels.color {String / Array / Object}: Optional Color of the canvas. See
colors specification.inter {String}: Optional interpolation method. Defaults to "lanczos".
Possible values:"nearest-neighbor""moving-average""linear""grid""cubic""lanczos"callback {Function(err, image)}Cover a canvas with the image. The image will be resized to the smallest possible size such that both its dimensions are bigger than the canvas's dimensions. Margins of the image exceeding the canvas will be discarded.
image.cover(width, height, inter, callback)
width {Integer}: Canvas' width in pixels.height {Integer}: Canvas' height in pixels.inter {String}: Optional interpolation method. Defaults to "lanczos".
Possible values:"nearest-neighbor""moving-average""linear""grid""cubic""lanczos"callback {Function(err, image)}image.rotate(degs, color, callback)
degs {Float}: Clockwise rotation degrees.color {String / Array / Object}: Optional Color of the canvas. See
colors specification.callback {Function(err, image)}image.crop(left, top, right, bottom, callback)
left, top, right, bottom {Integer}: Coordinates of the crop rectangle.callback {Function(err, image)}image.crop(width, height, callback)
width, height {Integer}: Width and height of the rectangle to crop from the
center of the image.callback {Function(err, image)}Gaussian blur.
image.blur(sigma, callback)
sigma {Float>=0}: Standard deviation of the Gaussian filter.callback {Function(err, image)}Inverse diffusion shapren.
image.sharpen(amplitude, callback)
amplitude {Float}: Sharpening amplitude.callback {Function(err, image)}Mirror an image along the 'x' axis, 'y' axis or both.
image.mirror(axes, callback)
axes {String}: 'x', 'y' or 'xy' (case sensitive).callback {Function(err, image)}Alias of mirror.
Add a colored border to the image.
image.border(width, color, callback)
width {Integer}: Border width in pixels.color {String / Array / Object}: Optional Color of the border. See
colors specification.callback {Function(err, image)}Pad image edges with colored pixels.
image.pad(left, top, right, bottom, color, callback)
left, top, right, bottom {Integer}: Number of pixels to add to each edge.color {String / Array / Object}: Optional Color of the padding. See
colors specification.callback {Function(err, image)}Adjust image saturation.
image.saturate(delta, callback)
delta {Float}: By how much to increase / decrease the saturation.callback {Function(err, image)}Examples:
image.saturate(0, ...) will have no effect on the image.image.saturate(0.5, ...) will increase the saturation by 50%.image.saturate(-1, ...) will decrease the saturation by 100%, effectively
desaturating the image.Adjust image lightness.
image.lighten(delta, callback)
delta {Float}: By how much to increase / decrease the lightness.callback {Function(err, image)}Examples:
image.lighten(0, ...) will have no effect on the image.image.lighten(0.5, ...) will increase the lightness by 50%.image.lighten(-1, ...) will decrease the lightness by 100%, effectively
making the image black.