pngjs
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
PNGJS is a pure JavaScript PNG encoder and decoder used in Node.js with zero dependencies. This library supports any bit size from 1,2,4 & 16, interlace, async and sync files. It brings enhancements over the original pngjs, such as support for reading tTRNS transparent colours and writing a variety of color types. Notably, a synchronous interface alongside the asynchronous one is provided, and the API is compatible with pngjs and node-pngjs. Although it is highly functional, it does not offer support for extended PNG, such as animation or writing in colortype 3 (indexed color).
To use pngjs, you first have to install it via npm using the command npm install pngjs --save
. Once installed, you can include it in your project by importing PNG from pngjs. Here's an example of how to use it:
const fs = require("fs");
const PNG = require("pngjs").PNG;
fs.createReadStream("in.png")
.pipe(
new PNG({
filterType: 4,
})
)
.on("parsed", function () {
for (let y = 0; y < this.height; y++) {
for (let x = 0; x < this.width; x++) {
let idx = (this.width * y + x) << 2;
// invert color
this.data[idx] = 255 - this.data[idx];
this.data[idx + 1] = 255 - this.data[idx + 1];
this.data[idx + 2] = 255 - this.data[idx + 2];
// and reduce opacity
this.data[idx + 3] = this.data[idx + 3] >> 1;
}
}
this.pack().pipe(fs.createWriteStream("out.png"));
});
In this block of code, the script reads a PNG image "in.png", inverts its colors and reduces opacity, then writes the output to "out.png".
The documentation for pngjs which provides the detailed description, features, and code usage examples, can be found on the GitHub repository of the pngjs project. The readme file on the GitHub page serves as the official documentation and provides all the necessary details needed to use this package. It is well-structured, offering a comparison table with other similar packages, details installation process, usage examples, and both Async and Sync API references, making it very straightforward to use and integrate in your JavaScript projects.