Instantly share code, notes, and snippets.

Embed
What would you like to do?
Reads folders of sequences of PNGs. Exports oodles of optimized GIFs at different sizes and crops to post at different internets places.
//REQUIREMENTS: The following programs must be installed and accessible in your PATH
// node > 6
// imagemagick's command line tool "convert"
// gifsicle
//USAGE: Change params at the top to your liking. `node gif_process.js` starts it up.
// EXECUTES ALL TASKS IN PARALEL.
// If you run it like this, it will CONSUME YOUR WHOLE COMPUTER (100% cpu, 100% memory) for a few minutes while it works.
//DISCLOSURE: I was running terribly low on sleep when I wrote this. It's a tool. It gets a job done like ten times ever. Don't judge me on this. The code I write for my day job is like way better and I care a lot more about it because I have to maintain it all the time.
//LICENSE: MIT
let imPath = 'script_output-imagemagick/';
let gsPath = 'script_output-gifsicle/';
let seriesName = 'rainbow_torus_spiral';
let inputPngFolderList = [
'01-png_rgba',
'02-png_rgba',
'02-png_rgba-1',
'02-png_rgba-2',
'02-png_rgba-3',
'02-png_rgba-4',
'04-png_rgba',
];
let outputParams = [ //<-- only do like 1 set at a time for sanity
{width: 960},
{width: 800, height: 600},
{width: 800, height: 600, resize: 540, colors: 240},
{width: 480},
{width: 400},
];
let backgroundColor = '#262626';
/*------------------------------------------------*/
const fs = require('fs');
const child_process = require('child_process');
let makePath = function(path){
if(!fs.existsSync(path)){
fs.mkdirSync(path);
}
};
makePath(imPath);
makePath(gsPath);
let timeTask = function(taskName, callback){
var start = Date.now();
return function(err, stdout, stderr) {
var time = (Date.now() - start);
console.log([taskName, stdout, time, 'ms'].join(' ').replace(/\n/g, ' '));
if (err) {
console.error(err);
return;
} else if(callback) {
callback();
}
};
};
let hellYeahGifProcess = function(x, y, scale, c){
let width = x;
let height = y || x;
let resize = scale || y || x;
let colors = c || 256;
let leftRight = (width - resize) / 2;
let topBottom = (height - resize) / 2;
let scaledCount = 0;
let dimensions = `${width}x${height}`;
if(scale){
dimensions += `x${scale}`;
}
if(c){
dimensions += `-colors_${c}`;
}
let imSizePath = imPath + dimensions + '/';
let gsSizePath = gsPath + dimensions + '/';
makePath(imSizePath);
makePath(gsSizePath);
let imList = [];
let gsList = [];
let gifSicleImagesWhenDone = function(){
imList.forEach(function(outputFileNameIM, index){
let outputFileNameGS = gsList[index];
child_process.exec(
[
'gifsicle',
'-O3',
`-o ${outputFileNameGS}`,
outputFileNameIM
].join(' '),
timeTask(outputFileNameGS)
);
});
};
let onComplete = function(){
scaledCount++;
if(scaledCount === inputPngFolderList.length){
gifSicleImagesWhenDone();
}
};
inputPngFolderList.forEach(function(inputFileName){
let name = `${seriesName}-${inputFileName}-${dimensions}`;
name += '-imagemagick';
let outputFileNameIM = imSizePath + name + '.gif';
let outputFileNameGS = gsSizePath + name + '-gifsicle.gif';
imList.push(outputFileNameIM);
gsList.push(outputFileNameGS);
child_process.exec(
[
'convert',
'-dispose 2',
'-delay 1/24',
inputFileName + '/*.png',
'-coalesce',
'-crop 1080x1080+420+0',
'-resize ' + resize,
`-background "${backgroundColor}"`,
'-alpha remove',
`-bordercolor "${backgroundColor}"`,
`-border ${leftRight}x${topBottom}`,
`-colors ${colors}`,
'+repage',
outputFileNameIM
].join(' '),
timeTask(outputFileNameIM, onComplete)
);
});
};
outputParams.forEach(function(params){
hellYeahGifProcess(params.width, params.height, params.resize, params.colors);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment