Skip to content

Instantly share code, notes, and snippets.

@bertrandom
Created December 17, 2016 08:13
Show Gist options
  • Save bertrandom/8f83fce59482571face586056b3d4db0 to your computer and use it in GitHub Desktop.
Save bertrandom/8f83fce59482571face586056b3d4db0 to your computer and use it in GitHub Desktop.
const async = require('async');
var gm = require('gm');
var sprintf = require("sprintf-js").sprintf;
var faces = require('./faces.json');
var bumps = [];
for (var i = 1; i <= 66; i++) {
bumps.push(i.toString());
}
async.eachSeries(bumps, function getFace(item, callback) {
if (faces[item] && faces[item][0] && faces[item][0].faceRectangle) {
var coords = faces[item][0].faceRectangle;
var img = gm('./images/babybump' + item + '.jpg');
img.size(function(err, size) {
var scalingFactor = (coords.width / 150);
var newWidth = (Math.round(size.width / scalingFactor));
coords.left = Math.round(coords.left / scalingFactor);
coords.top = Math.round(coords.top / scalingFactor);
coords.width = Math.round(coords.width / scalingFactor);
coords.height = Math.round(coords.height / scalingFactor);
var offsetX = Math.floor(coords.left - 500);
if (offsetX < 0) {
offsetX = '-' + Math.abs(offsetX);
} else {
offsetX = '+' + offsetX;
}
var offsetY = Math.floor(coords.top - 500);
if (offsetY < 0) {
offsetY = '-' + Math.abs(offsetY);
} else {
offsetY = '+' + offsetY;
}
var extentOptions = offsetX + offsetY;
img
.resize(newWidth)
.background('black')
.extent(4096, 4096, extentOptions)
.crop(1100,1700,100,310)
.write('./tmp/babybump' + sprintf('%02f', item) + '.jpg', function (err) {
callback();
});
});
} else {
async.setImmediate(function() {
callback();
});
}
}, function done(){
console.log('All done!');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment