Dan*_*obe 2 javascript png image-processing node.js
我在从https://github.com/niegowski/node-pngjs此处的文档中找出创建PNG文件(编码)时遇到麻烦。该文档提供了有关处理现有PNG的示例。
为了进行健全性检查,我一直在尝试为每个像素赋予相同的RGB值。我认为这个问题帖子也很相关https://github.com/niegowski/node-pngjs/issues/20。我尝试过的继承人代码
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);
});
Run Code Online (Sandbox Code Playgroud)
简单:
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'));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3155 次 |
| 最近记录: |