Q:

save-pixels leading to corrupted and desaturated images on Node.js

I am using save-pixels and get-pixels npm modules to get an ndarray from a DataURL and save pixels to convert an image ndarray to a DataURL. But I am facing a few issues while using save-image to make a DataURL. Am I doing this correctly? Is there a better way to do this?
This is the code I am using:
var buffer = require('fs').createWriteStream('output.txt');
var enc = require('base64-stream').encode();
savePixels(pixels, 'png').on('end', function() {
    //Writes a DataURL to  output.txt
    buffer.write("data:image/png;base64,"+enc.read().toString());
}).pipe(enc);

Issues I Get : The image becomes desaturated and DataURL of PNG Images is incomplete.
For complete reference:
http://github.com/publiclab/image-sequencer/issues/25

A:

The callback should be on the piping and not the savePixels. That was leading to incomplete images.
So the code should be this:
var buffer = require('fs').createWriteStream('output.txt');
var enc = require('base64-stream').encode();
savePixels(pixels, 'png').pipe(enc).on('end', function() {
    //Writes a DataURL to  output.txt
    buffer.write("data:image/png;base64,"+enc.read().toString());
});

