Last active
January 31, 2024 20:17
-
-
Save xNok/74934a0c1c8500bfe84c8de6e92386cd to your computer and use it in GitHub Desktop.
convert all psd files in a folder into png files
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
// https://gist.github.com/xNok/74934a0c1c8500bfe84c8de6e92386cd | |
var PSD = require('psd'); | |
var fs = require('fs'); | |
// command usage validation | |
if (process.argv.length <= 2) { | |
console.log("Usage: " + __filename + " path/to/directory/ path/to/output_dir/"); | |
process.exit(-1); | |
} | |
var path = process.argv[2]; | |
var output = process.argv.length >= 3 ? process.argv[3] : null; | |
fs.readdir(path, function(err, items) { | |
for (var i=0; i<items.length; i++) { | |
psd2png(path + items[i], output) | |
} | |
}); | |
function psd2png(input, output){ | |
String.prototype.replaceAll = function(search, replacement) { | |
var target = this; | |
return target.replace(new RegExp(search, 'g'), replacement); | |
}; | |
String.prototype.basename = function() { | |
var str = this; | |
var base = new String(str).substring(str.lastIndexOf('/') + 1); | |
if(base.lastIndexOf(".") != -1) | |
base = base.substring(0, base.lastIndexOf(".")); | |
return base; | |
}; | |
if ( output == null ){ | |
output = input.replaceAll("psd", "png"); | |
}else{ | |
output = output + input.basename() + ".png"; | |
} | |
console.log("converting " + input + " to " + output) | |
PSD.open(input).then(function (psd) { | |
return psd.image.saveAsPng(output); | |
}).then(function () { | |
console.log("Finished!"); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment