Created
October 14, 2022 23:28
-
-
Save mstevenson/ada130813d75644da462cf917dd41410 to your computer and use it in GitHub Desktop.
Photoshop script to crop and resize images for Stable Diffusion training
This file contains hidden or 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
#target photoshop | |
// open a folder of images | |
var folder = Folder.selectDialog("Select a folder of JPGs to crop"); | |
var files = folder.getFiles("*.jpg"); | |
// create a folder to save the files to if it doesn't exist | |
var saveFolder = new Folder(folder + "/cropped"); | |
if (!saveFolder.exists) saveFolder.create(); | |
// iterate over each image in the folder | |
for (var i = 0; i < files.length; i++) { | |
var file = files[i]; | |
var doc = open(file); | |
doc = app.activeDocument; | |
var bounds, left, top, right, bottom; | |
// crop | |
if (doc.height > doc.width) { // portrait | |
left = 0; | |
top = (doc.height-doc.width)/2; | |
right = doc.width; | |
bottom = top + doc.width; | |
} else { // landscape | |
left = (doc.width-doc.height)/2; | |
top = 0; | |
right = left + doc.height; | |
bottom = doc.height; | |
} | |
bounds = [left, top, right, bottom]; | |
doc.crop(bounds); | |
// resize to 512x512 | |
doc.resizeImage(512, 512, 72, ResampleMethod.BICUBIC); | |
// save a copy | |
var saveFile = new File(doc.path + "/cropped/" + doc.name); | |
var saveOptions = new JPEGSaveOptions(); | |
saveOptions.quality = 12; | |
doc.saveAs(saveFile, saveOptions, true, Extension.LOWERCASE); | |
doc.close(SaveOptions.DONOTSAVECHANGES); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment