Liteweight png image processing library.
Note: since v2.0.0
this lib based on pngjs2 and doesn't build any native modules.
npm install png-img
Create PngImg
object from passed buffer with image.
Arguments:
buf
-Buffer
with image file contentcb
- callback. Will be called when image will be parsed
var fs = require('fs'),
PngImg = require('png-img');
var buf = fs.readFileSync('path/to/img.png');
PngImg.fromBuffer(buf, function(err, img) {
if (err) {
console.error(err);
} else {
// Use `img`
}
});
Get image size as an object.
console.log(img.size());
for 32x32 image will print out:
{ width: 32, height: 32 }
Get pixel color and alpha.
Returns object:
- r: red channel (0 to 255)
- g: green channel (0 to 255)
- b: blue channel (0 to 255)
- a: alpha (0 to 255). 0 - transparent
console.log(img.get(0, 0));
will print pixel and color for pixel (0, 0):
{
r: 100,
g: 150,
b: 200,
a: 255
}
Fill region with passed color. Modifies current image.
Arguments:
offsetX
- horizontal offset from the left side of the imageoffsetY
- vertical offset from the top side of the imagewidth
- region widthheight
- region heightcolor
- color as {r,g,b,a} object or as a '#XXXXXX' string
Returns: current image object
Throws if region is not inside the current image
img
.fill(0, 0, 16, 16, '#00ffFF') // fill with cyan
.fill(16, 16, 16, 16, {r: 0, g: 255, b: 255, a: 127}); // fill with half-transparent cyan
Same as fill(x, y, 1, 1, color)
Crop image. Modifies current image.
Arguments:
offsetX
- horizontal offset from the left side of the imageoffsetY
- vertical offset from the top side of the imagewidth
- new widthheight
- new height
Returns: current image object
Throws if new image is not inside the current image.
img
.crop(0, 0, 16, 16)
.crop(8, 8, 8, 8);
Save image to file. Asynchronous operation.
Arguments:
file
- path to file to save imagecallback
- function with one argument (error
). Will be called after save operation finish or on error.
Overwrites existing file.
img.save('path/to/file.png', function(error) {
if(error) {
console.error('Error:', error);
} else {
console.log('OK');
}
});