Translate a crop area from a zoomed copy
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
// 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