Code Monkey home page Code Monkey logo

png-stream's Introduction

png-stream

A streaming PNG encoder and decoder for Node and the browser. Supports animated PNGs and normal still PNGs.

Installation

npm install png-stream

For the browser, you can build using Browserify.

Decoding

This example uses the concat-frames module to collect the output of the PNG decoder into an array of frame objects.

var PNGDecoder = require('png-stream/decoder');
var concat = require('concat-frames');

// decode a PNG file to RGB pixels
fs.createReadStream('in.png')
  .pipe(new PNGDecoder)
  .pipe(concat(function(frames) {
    // frames is an array of frame objects
    // each one has a `pixels` property containing
    // the raw RGB pixel data for that frame, as
    // well as the width, height, etc.
  }));

Encoding

You can encode a PNG by writing or piping pixel data to a PNGEncoder stream. The PNG encoder supports writing data in the RGB, RGBA, grayscale (gray), and grayscale + alpha (gray) color spaces. You can also write data in the indexed color space by first quantizing it using the neuquant module.

var PNGEncoder = require('png-stream/encoder');
var neuquant = require('neuquant');

// convert a JPEG to a PNG
fs.createReadStream('in.jpg')
  .pipe(new JPEGDecoder)
  .pipe(new PNGEncoder)
  .pipe(fs.createWriteStream('out.png'));
  
// write indexed data
fs.createReadStream('in.jpg')
  .pipe(new JPEGDecoder)
  .pipe(new neuquant.Stream)
  .pipe(new PNGEncoder)
  .pipe(fs.createWriteStream('indexed.png'));

License

MIT

png-stream's People

Contributors

devongovett avatar goto-bus-stop avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

png-stream's Issues

Help encoding indexed png to file

I tried encoding a basic png, using the test file as an example

var PNGEncoder = require('png-stream/encoder');
var fs = require('fs');


var palette = new Buffer([ 256 ]);
var pixels = new Buffer(10 * 10);
pixels.fill(1);

var enc = new PNGEncoder(10, 10, { colorSpace: 'indexed', palette: palette })
   .pipe(fs.createWriteStream('out.png'))
   .end(pixels);

My os says that the file is corrupted though. Where am I going wrong?

convert png to jpg do not get the right picture

the png is this:

test image

code:

  fs.createReadStream(file)
  .pipe(new PNGDecoder)
  //.pipe(new neuquant.Stream)
  .pipe(new JPEGEncoder)
  .pipe(fs.createWriteStream('indexed.jpg'));

and the convert result :
result image

Error: Requested indexed color space without palette

I tried exactly the code in the readme. However I encountered an error

Exception has occurred: Error
Error: Requested indexed color space without palette
    at PNGEncoder._start (/Users/xxx/Documents/sss/node_modules/png-stream/encoder.js:65:17)
    at write (/Users/xxx/Documents/sss/node_modules/pixel-stream/index.js:84:14)
    at PNGEncoder.PixelStream._transform (/Users/xxx/Documents/sss/node_modules/pixel-stream/index.js:154:3)
    at PNGEncoder.Transform._read (_stream_transform.js:186:10)
    at PNGEncoder.Transform._write (_stream_transform.js:174:12)
    at doWrite (_stream_writable.js:385:12)
    at writeOrBuffer (_stream_writable.js:371:5)
    at PNGEncoder.Writable.write (_stream_writable.js:288:11)
    at NeuQuantStream.ondata (_stream_readable.js:642:20)
    at emitOne (events.js:115:13)

I am using Node 8.4.0 on macOs Sierra 10.12.6

Code

const PNGEncoder = require('png-stream/encoder')
const JPEGDecoder = require('jpg-stream/decoder')
const fs = require('fs')
const neuquant = require('neuquant')

fs.createReadStream(input)
        .pipe(new JPEGDecoder)
        .pipe(new neuquant.Stream)
        .pipe(new PNGEncoder)
        .pipe(fs.createWriteStream(input+'indexed.png'))

How to decode and then encode the decoded data

I am struggling to figure out how to read a stream from one file, editing the pixel values and then writing to another. I have tried:

    fs.createReadStream('in.png')
    .pipe(new PNGDecoder)
    .pipe(concat(function(frames) {
                     //Editing the pixel values in here;
    }))
    .pipe(new PNGEncoder)
    .pipe(fs.createWriteStream('out.png'));

and also this:

fs.createReadStream('default.png')
    .pipe(new PNGDecoder)
    .pipe(concat(function(frames) {

        var encoder = new PNGEncoder(100,100,{animated: true});
                         .pipe(fs.createWriteStream('out.png'));

        for(var i = 0; i < frames.length; i++){
            var pixels = frames[i].pixels;

            for(var j = 0; j < pixels.length; j+=4){
                var r = pixels[j];
                var g = pixels[j+1];
                var b = pixels[j+2];
                var a = pixels[j+3];

                r = Math.max(255, r + 20);

                frames[i].pixels[j] = r;
                frames[i].pixels[j+1] = g;
                frames[i].pixels[j+2] = b;
                frames[i].pixels[j+3] = a;
            }

            encoder.write(frames[i]);
        }

        encoder.end();

    }));

Neither of which work.

It would be great if there was some documentation as to how to use the encoder...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.