Skip to content

Instantly share code, notes, and snippets.

@hugohil hugohil/scale-crop.js
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.