本文介绍了如何使用PNGJS库从rgb矩阵创建png?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法从此处的文档中找出创建PNG文件(编码)的信息。该文档提供了一个操作现有PNG的示例。
为了理智检查,我一直试图给每个像素提供相同的RGB值。我认为这个问题的帖子也是相关的。下面是我试过的代码
Im having trouble figuring out to create a PNG file (encoding) from the documentation here https://github.com/niegowski/node-pngjs . The documentation gives an example on manipulating an existing PNG.
In order to sanity check I've been trying to give every pixel the same RGB value. I think this issue post is relevant as well https://github.com/niegowski/node-pngjs/issues/20 . Heres a code Ive tried
var pic = new PNG({
width: cols,
height: rows
});
console.log(pic.width, pic.height);
var writeStream = fs.createWriteStream('C:\\Users\\yako\\desktop\\out.png');
pic.pack().pipe(writeStream);
writeStream.on('parsed', function() {
console.log('parsed');
});
writeStream.on('finish', function() {
fs.createReadStream('C:\\Users\\yako\\desktop\\out.png')
.pipe(new PNG({
filterType: -1
}))
.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;
this.data[idx] = 255;
this.data[idx+1] = 218;
this.data[idx+2] = 185;
this.data[idx+3] = 0.5;
}
}
this.pack().pipe(fs.createWriteStream('C:\\Users\\yako\\desktop\\newOut.png'));
});
});
writeStream.on('error', function (err) {
console.error(err);
});
推荐答案
简单:
var fs = require('fs'),
PNG = require('pngjs').PNG;
var png = new PNG({
width: 100,
height: 100,
filterType: -1
});
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+1] = 218;
png.data[idx+2] = 185;
png.data[idx+3] = 128;
}
}
png.pack().pipe(fs.createWriteStream('newOut.png'));
这篇关于如何使用PNGJS库从rgb矩阵创建png?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!