Skip to content

Instantly share code, notes, and snippets.

@radiodario
Forked from mildfuzz/trimSouthEast.js
Last active December 24, 2015 14:09
Show Gist options
  • Save radiodario/6810535 to your computer and use it in GitHub Desktop.
Save radiodario/6810535 to your computer and use it in GitHub Desktop.
Helping fuzzy at #node.js work with imagemagick
trimmer = require('./trimSouthEast.js');
var files = ['lol lol.png']
files.forEach(trimmer.trimSouthEast);
var spawn = require('child_process').spawn;
var fs = require('fs');
var os = require('os');
exports.trimSouthEast = function(file, i){
/*
* Trims the bottom and right edges
*/
var origFile = process.cwd() + '/' + file;
var tempFile1 = os.tmpDir() + 'image1_' +i+'.png';
var tempFile2 = os.tmpDir() + 'image2_' +i+'.png';
var args1 = [origFile, "-gravity", "North", "-background", "white", "-splice", "0x1", "-background", "black", "-splice", "0x1","-trim","+repage","-chop", "0x1", tempFile1];
var args2 = [tempFile1, "-gravity", "West", "-background", "white", "-splice", "1x0", "-background", "black", "-splice", "1x0","-trim","+repage", "-chop", "1x0", tempFile2];
var firstTrans = spawn("convert", args1);
firstTrans.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
firstTrans.stderr.on('data', function (data) {
console.error('stderr: ' + data);
});
firstTrans.on("close", function(code) {
console.log('first transform process exited with code ' + code);
if (code == 0) {
var secondTrans = spawn("convert", args2);
secondTrans.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
secondTrans.stderr.on('data', function (data) {
console.error('stderr: ' + data);
});
secondTrans.on("close", function(code) {
console.log('second transform process exited with code ' + code);
if (code == 0) {
fs.rename(tempFile2, origFile, function(err){
if (err) {
console.error("I couldn't process file", origFile, err)
}
});
}
});
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment