我正在尝试从图像数组中生成de gif。我使用节点JS。我可以创建gif,但是gif不正确。我将gif结果附加到了问题上。我希望从图像表中生成带有动画的gif
(async () => {
const GIFEncoder = require('gif-encoder');
const encoder = new GIFEncoder(800, 600);
const fs = require('fs');
const getPixels = require('get-pixels');
let file = require('fs').createWriteStream('mygif.gif');
// Setup gif encoder parameters
encoder.setFrameRate(60);
encoder.pipe(file);
encoder.setQuality(40);
encoder.setDelay(500);
encoder.writeHeader();
encoder.setRepeat(0);
// Helper functions declaration
function addToGif(images, counter = 0) {
getPixels(images[counter], function (err, pixels) {
encoder.addFrame(pixels.data);
encoder.read();
if (counter === images.length - 1) {
encoder.finish();
cleanUp(images, function (err) {
if (err) {
console.log(err);
} else {
console.log('Gif created!');
process.exit(0);
}
});
} else {
addToGif(images, ++counter);
}
});
};
function cleanUp(listOfPNGs, callback) {
let i = listOfPNGs.length;
listOfPNGs.forEach(function (filepath) {
fs.unlink(filepath, function (err) {
i--;
if (err) {
callback(err);
return;
} else if (i <= 0) {
callback(null);
}
});
});
};
let listOfPNGs = [
'./temp/01.png',
'./temp/02.png',
'./temp/03.png'];
addToGif(listOfPNGs);
})();