What is pngjs?
The pngjs npm package is a pure JavaScript implementation for PNG encoding and decoding in Node.js. It allows for reading and writing PNG images in a non-blocking and streaming way, which is useful for applications that need to handle image data directly.
What are pngjs's main functionalities?
Reading PNG files
This code demonstrates how to read a PNG file using pngjs. It creates a read stream from a file, pipes it through the PNG decoder, and logs the image's width and height once it's parsed.
const fs = require('fs');
const PNG = require('pngjs').PNG;
fs.createReadStream('input.png')
.pipe(new PNG())
.on('parsed', function() {
console.log('Image width:', this.width, 'Image height:', this.height);
});
Writing PNG files
This code snippet shows how to create a new PNG image with a specific width and height, fill it with white color, and then write it to a file. It demonstrates the package's ability to write PNG files.
const fs = require('fs');
const PNG = require('pngjs').PNG;
const png = new PNG({ width: 10, height: 10, filterType: -1 });
for (let y = 0; y < png.height; y++) {
for (let x = 0; x < png.width; x++) {
let idx = (png.width * y + x) << 2;
png.data[idx] = 255; // red
png.data[idx + 1] = 255; // green
png.data[idx + 2] = 255; // blue
png.data[idx + 3] = 255; // alpha (opacity)
}
}
png.pack().pipe(fs.createWriteStream('output.png'));
Other packages similar to pngjs
sharp
Sharp is a high-performance Node.js image processing library. It allows for converting large images in common formats to smaller, web-friendly JPEG, PNG, WebP, and AVIF images of varying dimensions. Compared to pngjs, sharp supports a wider range of image formats and provides more comprehensive image processing capabilities, but it relies on native dependencies.
jimp
Jimp is an image processing library for Node.js, written entirely in JavaScript, with zero native dependencies. It can perform a variety of operations such as resize, crop, rotate, and color manipulation. While Jimp supports PNG among other formats, it offers broader functionality beyond just PNG encoding and decoding, making it more versatile than pngjs for general image processing tasks.
About
Simple PNG encoder/decoder for Node.js with no native dependencies.
Installation
$ npm install pngjs
Example
var fs = require('fs'),
PNG = require('pngjs').PNG;
var png = new PNG({
filterType: 4
});
png.on('parsed', function() {
for (var y = 0; y < png.height; y++) {
for (var x = 0; x < png.width; x++) {
var idx = (png.width * y + x) << 2;
png.data[idx] = 255 - png.data[idx];
png.data[idx+1] = 255 - png.data[idx+1];
png.data[idx+2] = 255 - png.data[idx+2];
png.data[idx+3] = png.data[idx+3] >> 1;
}
}
png.pipe(fs.createWriteStream('out.png'));
png.pack();
});
fs.createReadStream('in.png').pipe(png);
For more examples see examples
folder.
Documentation
As input any color type is accepted (grayscale, rgb, palette, grayscale with alpha, rgb with alpha) but 8 bit per sample (channel) is the only supported bit depth. Interlaced mode is not supported.
Supported ancillary chunks
gAMA
- gamma,tRNS
- transparency (but only for paletted image)
Class: PNG
PNG
is readable and writeable Stream
.
Options
width
- int
height
- int
checkCRC
- boolean
default: true
deflateChunkSize
- int
default: 32 kBdeflateLevel
- int
default: 9filterType
- int
default: -1 (auto)
Event: "parsed"
function(data) { }
png.pack()
Starts converting data to PNG file Stream
png.parse(data, [callback])
Parses PNG file data. Alternatively you can stream data to PNG.
Optional callback
is once called on error
or parsed
. The callback gets
two arguments (err, data)
.
Property: width
Property: height
Property: data
Buffer of image pixel data. Every pixel consists 4 bytes: R, G, B, A (opacity).
Property: gamma
Changelog
0.2.0-alpha - 21 Aug 2012
- Input added palette, grayscale, no alpha support
- Better scanline filter selection
0.1.0-alpha - 19 Aug 2012
License
(The MIT License)
Copyright (c) 2012 Kuba Niegowski
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.