Closed MichalPaszkiewicz closed 9 years ago
The second example is really close. It should work with encoder.write(frames[i].pixels)
. You can also use encoder.addFrame(frame)
before calling encoder.write
to set frame metadata, like the delay between frames, etc.
Unfortunately, this requires the entire PNG to be decoded before you do any encoding, which isn't necessary for your example. You are just modifying some pixels it looks like, so it would be better to do this in a streaming way so that the whole file and not even a whole frame needs to be decoded at once. To do this, you can write a pixel-stream implementation to transform the pixels, and then pipe the image through it before getting to the encoder (see below). For a good example of writing a transform stream that modifies pixels, see color-transform.
fs.createReadStream('in.png')
.pipe(new PNGDecoder)
.pipe(new MyTransformStream)
.pipe(new PNGEncoder)
.pipe(fs.createWriteStream('out.png'));
Awesome, thanks!
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:
and also this:
Neither of which work.
It would be great if there was some documentation as to how to use the encoder...