png.js产生的PNG噪声 [英] Noise in PNG produced by pngjs for nodejs

查看:279
本文介绍了png.js产生的PNG噪声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 pngjs v3.3.0(适用于nodejs)将PNG Spritesheet切片为Sprite.这使我产生了一些生成的精灵所不希望的嘈杂背景.然后,我尝试了一个简单的脚本,该脚本仅创建空的透明PNG,然后将其写入文件:

I want to slice PNG spritesheet to sprites using pngjs v3.3.0 for nodejs. This gives me unxpected noisy background for some of produced sprites. Then I tryed simple script that just creates empty transparent PNG and then writes it to a file:

var fs          = require('fs');
var PNG         = require('pngjs').PNG;
var dstBuffer   = new PNG({ width:50, height:50 });

dstBuffer.pack().pipe(fs.createWriteStream("empty.png"));

并且还会创建带有嘈杂背景的空PNG.我还尝试不使用透明度(colorType:2),然后得到带有噪点的白色PNG.每次我使用脚本时,噪音都会有所不同. 外观是.尝试过同步和异步方法.

And empty PNG is created with noisy background as well. I also tried to not to use transparency (colorType:2), then I got white PNG with noise on it. And every time I use the script, noise differs. This is how it looks like. Tried sync and async approach.

我仅在cmd中运行脚本,如下所示:node pngjs. pngjs仅通过npm:npm install pngjs安装.所以我需要避免在背景上出现这种噪音.

I run sccript simply in cmd as follows: node pngjs. And pngjs installed just with npm: npm install pngjs. So I need to avoid this noise on background.

推荐答案

数据需要初始化.可以这样实现(另请参见 https://github.com /lukeapage/pngjs/blob/master/examples/newfile.js ):

The data needs to be initialized. This can be achieved like this (also see https://github.com/lukeapage/pngjs/blob/master/examples/newfile.js):

var fs          = require('fs');
var PNG         = require('pngjs').PNG;
var dstBuffer   = new PNG({ width:50, height:50 });

for (var y = 0; y < dstBuffer.height; y++) {
    for (var x = 0; x < dstBuffer.width; x++) {
        var idx = (dstBuffer.width * y + x) << 2;

        dstBuffer.data[idx] = 255;
        dstBuffer.data[idx + 1] = 255;
        dstBuffer.data[idx + 2] = 255;

        dstBuffer.data[idx + 3] = 255;
    }
}

dstBuffer.pack().pipe(fs.createWriteStream("empty.png"));

这篇关于png.js产生的PNG噪声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆