使用Node JS将多个JPG传送到动画GIF中 [英] Pipe multiple JPG's into an animated GIF using Node JS

查看:347
本文介绍了使用Node JS将多个JPG传送到动画GIF中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将5个特定的JPG文件名传送到动画GIF中.我浏览了几个库,发现 Gif编码器.我对流不是很好.我似乎无法弄清楚如何将 JPEG解码器的RGB结果输送到addFrame( )的编码器方法.

I am trying to pipe 5 specific JPG file names into an animated GIF. I have looked through several libraries and found Gif-Encoder. I am not very good with streams. I cannot seem to figure out how to pipe the RGB result from the JPEG Decoder into the addFrame() method of the encoder.

function createAnimatedGif(jpgPaths, animatedGifPath) {
    let encoder = new GifEncoder(1280, 720);

    let writeStream = fs.createWriteStream(animatedGifPath);
    encoder.pipe(writeStream);
    encoder.writeHeader();

    jpgPaths.forEach(filePath => {
        fs.createReadStream(filePath)
            .pipe(new JPEGDecoder)
            //.pipe(encoder);
            //.pipe(pixels => encoder.addFrame(pixels));
    });

    encoder.finish();
}

推荐答案

是否必须使用读取流?我使用模块"get-pixels"取得了一些成功.

Is using a read streams a must? I had some success using the module 'get-pixels'.

var getPixels = require('get-pixels')
var GifEncoder = require('gif-encoder');
var gif = new GifEncoder(1280, 720);
var file = require('fs').createWriteStream('img.gif');
var pics = ['./pics/1.jpg', './pics/2.jpg', './pics/3.jpg'];

gif.pipe(file);
gif.setQuality(20);
gif.setDelay(1000);
gif.writeHeader();

var addToGif = function(images, counter = 0) {
  getPixels(images[counter], function(err, pixels) {
    gif.addFrame(pixels.data);
    gif.read();
    if (counter === images.length - 1) {
      gif.finish();
    } else {
      addToGif(images, ++counter);
    }
  })
}
addToGif(pics);

这篇关于使用Node JS将多个JPG传送到动画GIF中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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