I would like to make all of the pictures to aspect 1:1 by adding white background.
By adjusting each picture to it's biggest height OR width and NOT cropping or changing the size of the actual picture. The script I have does not work and errors out. I am not sure why. Can anyone help out or do you have a script that does what I am looking to do?
By adjusting each picture to it's biggest height OR width and NOT cropping or changing the size of the actual picture. The script I have does not work and errors out. I am not sure why. Can anyone help out or do you have a script that does what I am looking to do?
JavaScript:
// Open folder selection dialog
var inputFolder = Folder.selectDialog("Select a folder with images");
// Check if a valid folder was selected
if (inputFolder) {
var files = inputFolder.getFiles(/\.(jpg|jpeg|png|tif|tiff|psd)$/i);
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (file instanceof File) {
open(file);
// Get the active document
var doc = app.activeDocument;
// Get original width and height
var originalWidth = doc.width;
var originalHeight = doc.height;
// Determine the new canvas size (largest dimension)
var newSize = Math.max(originalWidth, originalHeight);
// Resize canvas to make it square (1:1 ratio)
doc.resizeCanvas(newSize, newSize, AnchorPosition.MIDDLECENTER);
// Deselect any active selection
doc.selection.deselect();
// Select the original image layer
var imageLayer = doc.activeLayer;
// Create a new solid white background layer
var whiteBackground = doc.artLayers.add();
whiteBackground.name = "White Background";
// Fill the new layer with white
doc.activeLayer = whiteBackground;
var white = new SolidColor();
white.rgb.red = 255;
white.rgb.green = 255;
white.rgb.blue = 255;
doc.selection.selectAll();
doc.selection.fill(white);
doc.selection.deselect();
// Move the white background layer to the **bottom**
whiteBackground.move(doc.artLayers[doc.artLayers.length - 1], ElementPlacement.PLACEATEND);
// Ensure the original image stays on top
imageLayer.move(doc.artLayers[0], ElementPlacement.PLACEATBEGINNING);
// Flatten the image to merge layers correctly
doc.flatten();
// Save the modified image
var saveFile = new File(file.path + "/" + file.name);
var saveOptions = new JPEGSaveOptions();
saveOptions.quality = 12; // Max quality
doc.saveAs(saveFile, saveOptions, true, Extension.LOWERCASE);
// Close the document without saving additional changes
doc.close(SaveOptions.DONOTSAVECHANGES);
}
}
alert("Batch processing completed!");
} else {
alert("No folder selected. Process canceled.");
}