Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Creates smaller square thumbnail images with correct appended file names, given a folder of original JPEG, PNG and GIF files at large sizes
// explanatory article: https://demosthenes.info/blog/993/1x-2x-3x-more-Batch-Processing-Retina-Images-with-PhotoShop-and-JavaScript
var inputFolder = Folder.selectDialog("Select a folder to process"),
outputFolder = Folder.selectDialog("Select a folder for the output files"),
imageSizes = [
["250px", "250px", "1x"],
["125px", "125px", "icon-2x"],
["75px", "75px", "icon-1x"]
],
numImageSizes = imageSizes.length;
if (inputFolder != null && outputFolder != null) {
var fileList = inputFolder.getFiles(/\.(jpg|jpeg|png|gif)$/i);
for(var i=0; i<fileList.length; i++) {
var doc = app.open( fileList[i] );
for (var j = 0; j < imageSizes.length; j++) {
var currentImageSize = imageSizes[j],
currentImageWidth = currentImageSize[0],
currentImageHeight = currentImageSize[1],
currentImageVersion = currentImageSize[2],
fullname = doc.name,
filename = fullname.substr(0, fullname.lastIndexOf('.')) || fullname,
extension = fullname.split('.').pop(),
exportOptionsSaveForWeb = new ExportOptionsSaveForWeb();
doc.resizeImage(currentImageWidth, currentImageHeight);
exportOptionsSaveForWeb.includeProfile = true;
exportOptionsSaveForWeb.optimized = true;
if (extension == "jpg" || extension == "jpeg") {
exportOptionsSaveForWeb.format = SaveDocumentType.JPEG;
exportOptionsSaveForWeb.includeProfile = true;
exportOptionsSaveForWeb.quality = 25;
}
if (extension == "png") {
exportOptionsSaveForWeb.format = SaveDocumentType.PNG;
}
if (extension == "gif") {
exportOptionsSaveForWeb.format = SaveDocumentType.GIF;
}
var documentPath = decodeURI(outputFolder) + "/" + filename + "-" + currentImageVersion + "." + extension,
file = new File(documentPath);
doc.exportDocument (file, ExportType.SAVEFORWEB, exportOptionsSaveForWeb);
}
doc.close(SaveOptions.DONOTSAVECHANGES);
}
}
@DanBurkhardt

This comment has been minimized.

Copy link

@DanBurkhardt DanBurkhardt commented Sep 27, 2020

Hello! Thanks for the excellent example. Had no idea this was so easy to automate.

I adapted your script into a version that is useful for automating Xcode .xcasset formats (for iOS developers)
Posted here: https://gist.github.com/DanBurkhardt/a30e4fa89fa2b694627f2fa5ad1de3d3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment