Last active
June 19, 2017 13:41
-
-
Save victor-shelepen/ef843efe74bde05433f73dec6b1378f9 to your computer and use it in GitHub Desktop.
This script tiles body images and convert them into an animation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cli = require('cli'); | |
var options = cli.parse({}, ['gm-tile', 'jimp-tile', 'ocv']); | |
var fs = require('fs'); | |
var path = require('path'); | |
var GIFEncoder = require('gifencoder'); | |
if (cli.command == 'ocv') { | |
var cv = require('opencv'); | |
console.log('opencv - hello!'); | |
} | |
else if (cli.command == 'jimp-tile') { | |
var Jimp = require('jimp'); | |
var title = 'Viktor 31'; | |
var bsmFolders = fs.readdirSync('./source'); | |
var heightPropotion = 9; | |
var widthPropotion = 5; | |
var propotionScale = 50; | |
var tileWidth = widthPropotion * propotionScale; | |
var tileHeight = heightPropotion * propotionScale; | |
var canvasWidth = tileWidth * 3; | |
var canvasHeight = tileHeight * 2; | |
var gifPath = path.join('./build', 'slides.gif') | |
var tileMargins = { | |
t: { | |
f: { | |
left: 0, | |
top: 0 | |
}, | |
b: { | |
left: 1, | |
top: 0 | |
} | |
}, | |
r: { | |
f: { | |
left: 0, | |
top: 1 | |
}, | |
b: { | |
left: 1, | |
top: 1 | |
}, | |
l: { | |
left: 2, | |
top: 0 | |
}, | |
r: { | |
left: 2, | |
top: 1 | |
} | |
} | |
}; | |
function mergeImageInto(canvasImage, state, view, basePath) { | |
var leftMargin = tileMargins[state][view]['left'] * tileWidth; | |
var topMargin = tileMargins[state][view]['top'] * tileHeight; | |
return Jimp.read(path.join(basePath, state + '-' + view + '.jpg')) | |
.then(function (image) { | |
image | |
.exifRotate() | |
.resize(Jimp.AUTO, tileHeight) | |
.crop(0, 0, tileWidth, tileHeight); | |
canvasImage.blit(image, leftMargin, topMargin, 0, 0, tileWidth, tileHeight); | |
}); | |
} | |
var encoder = new GIFEncoder(canvasWidth, canvasHeight); | |
// stream the results as they are available into myanimated.gif | |
encoder.createReadStream().pipe(fs.createWriteStream(gifPath)); | |
encoder.start(); | |
encoder.setRepeat(-1); // 0 for repeat, -1 for no-repeat | |
encoder.setDelay(2000); // frame delay in ms | |
encoder.setQuality(10); // image quality. 10 is default. | |
slidePromises = []; | |
slideImages = {}; | |
bsmFolders.sort().forEach(function (folderName, index) { | |
var basePath = path.join('./source', folderName); | |
var buildSlidePath = path.join('./build', folderName + '.gif[' + index + ']'); | |
if (index > 5 || ['.DS_Store'].indexOf(folderName) != -1) { | |
return; | |
} | |
var slidePromise = new Promise(function(resolve, reject) { | |
var slideImage = new Jimp(canvasWidth, canvasHeight, function (err, canvasImage) { | |
var tilePromises = []; | |
for (var stateKey in tileMargins) { | |
var stateMargins = tileMargins[stateKey]; | |
for (var viewKey in stateMargins) { | |
tilePromises.push(mergeImageInto(canvasImage, stateKey, viewKey, basePath)); | |
} | |
} | |
Promise.all(tilePromises) | |
.then(function() { | |
return Jimp.loadFont(Jimp.FONT_SANS_32_BLACK) | |
.then(function (font) { | |
canvasImage.print(font, 10, 10, title); | |
canvasImage.print(font, tileWidth * 2 + 10, 10, folderName); | |
}) | |
.then(function() { | |
resolve({ | |
folderName: folderName, | |
image: canvasImage | |
}) | |
}); | |
}) | |
}); | |
}); | |
slidePromise.then(function(imageData) { | |
slideImages[imageData['folderName']] = imageData['image']; | |
}); | |
slidePromises.push(slidePromise); | |
}); | |
Jimp.read('./_source/start.jpg') | |
.then(function(image) { | |
image.resize(canvasWidth, canvasHeight); | |
encoder.addFrame(image.bitmap.data); | |
return Promise.all(slidePromises) | |
.then(function() { | |
bsmFolders.sort().forEach(function(folderName) { | |
var image = slideImages[folderName]; | |
if (!!!image) { | |
return; | |
} | |
encoder.addFrame(image.bitmap.data); | |
console.log('Frame has been added.' + folderName); | |
}); | |
}) | |
}) | |
.then(function() { | |
return Jimp.read('./_source/finish.jpg') | |
.then(function (image) { | |
image.resize(canvasWidth, canvasHeight); | |
encoder.addFrame(image.bitmap.data); | |
encoder.finish(); | |
console.log('GIF saved'); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment