在Node.js中有没有一种简单的方法来读取PNG文件并获取图像的像素?类似于node-image,但是另一种方式:)
我浏览了https://github.com/joyent/node/wiki/modules#wiki-graphics上列出的库,但它们要么是提供裁剪和调整大小的命令行工具的简单包装器,要么是像node-canvas
这样的复杂绘图工具。
发布于 2012-11-27 22:24:15
这个可以同时进行PNG解码和编码,而不需要依赖于本机:
pngjs - PNG encoder/decoder for Node.js with no native dependencies.
反转PNG颜色的示例:
var fs = require('fs'),
PNG = require('pngjs').PNG;
fs.createReadStream('in.png')
.pipe(new PNG())
.on('parsed', function() {
for (var y = 0; y < this.height; y++) {
for (var x = 0; x < this.width; x++) {
var idx = (this.width * y + x) << 2;
// invert color
this.data[idx] = 255 - this.data[idx]; // R
this.data[idx+1] = 255 - this.data[idx+1]; // G
this.data[idx+2] = 255 - this.data[idx+2]; // B
// and reduce opacity
this.data[idx+3] = this.data[idx+3] >> 1;
}
}
this.pack().pipe(fs.createWriteStream('out.png'));
});
发布于 2012-08-14 01:23:42
我认为
var myimage = new PNG('myimage.png');
应该是
var myimage = new PNG.load('myimage.png');
https://stackoverflow.com/questions/11247790
复制相似问题