Skip to content

Instantly share code, notes, and snippets.

@hugohil
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugohil/1d0db5a2d8e2a54d28ad to your computer and use it in GitHub Desktop.
Save hugohil/1d0db5a2d8e2a54d28ad to your computer and use it in GitHub Desktop.
Translate a crop area from a zoomed copy
// Usage:
// node scale-crop.js infile zoom translate_x translate_y rotation outfile
'use strict';
var gm = require('gm');
var args = process.argv.slice(2);
console.log(args);
var infile = args[0];
var outfile = args[5];
var offset = 10;
var finalHeight = 720;
var zoom = args[1];
var translate_x = args[2];
var translate_y = args[3];
var rotation = args[4];
gm(infile).size(function (err, size){
if (err){
console.error(err);
} else {
var coeff = size.width / size.height;
var finalWidth = finalHeight * coeff;
var crop_w = size.width - (2 * (size.width / offset));
var crop_h = size.height - (2 * (size.height / offset));
var crop_x = (size.width / 2) - (crop_w / 2) + translate_x;
var crop_y = (size.height / 2) - (crop_h / 2) + translate_y;
gm(infile)
.scale(size.width * zoom)
.rotate('black', rotation)
.crop(crop_w, crop_h, crop_x, crop_y)
.resize(finalWidth, finalHeight)
.write(outfile, function (err){
if (err){
console.error(err);
} else {
console.log('Success!');
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment