What's new
Photoshop Gurus Forum

Welcome to Photoshop Gurus forum. Register a free account today to become a member! It's completely free. Once signed in, you'll enjoy an ad-free experience and be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Resize Canvas to 1:1 with White Background for all images in a folder


twiggs462

New Member
Messages
2
Likes
1
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?

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.");
}
 
Hi @twiggs462

A forum member may mump in with scripting capabilities with a good answer.
I did not try and run the script yet; sometimes, trying to put something under a Background Layer could throw a fault.

This could be done with an action, too.
Here are the verbal steps, and I assume this is a pixel Layer and a single Layer. Modifications could be made to cover those cases, too.

1) Make sure all selections are turned off
2) Make the background Layer a regular Layer
3) Duplicate the single regular Layer to the Second Layer
4) Transform the second Layer and rotate 90 degrees
5) Use the Image > Reveal All command and you will have boundaries that are now 1:1
6) Delete that Second Layer
7) Add white Layer at bottom
8) Merge back to single Layer
9) Save As file

Now you use the File > Automate > Batch command to access a folder of images and run this Action and use the Override Save As with your options as chosen in the Batch command

This was a quick write up yet this should work
Hope this was worth considering (avoiding scripts is worth considering)
John Wheeler
 
Try this revised code:


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) {
            try {
                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();

                // Store reference to the original image layer
                var imageLayer = doc.activeLayer;
                imageLayer.name = "Original Image"; // Name it for clarity

                // 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
                var bottomLayer = doc.artLayers[doc.artLayers.length - 1];
                whiteBackground.move(bottomLayer, ElementPlacement.PLACEAFTER);

                // Move the original image layer to the top
                var topLayer = doc.artLayers[0];
                imageLayer.move(topLayer, ElementPlacement.PLACEBEFORE);

                // 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);
            } catch (e) {
                alert("Error processing file: " + file.name + "\n" + e.message);
            }
        }
    }

    alert("Batch processing completed!");
} else {
    alert("No folder selected. Process canceled.");
}
 
This could be done with an action, too.
Here are the verbal steps, and I assume this is a pixel Layer and a single Layer. Modifications could be made to cover those cases, too.

1) Make sure all selections are turned off
2) Make the background Layer a regular Layer
3) Duplicate the single regular Layer to the Second Layer
4) Transform the second Layer and rotate 90 degrees
5) Use the Image > Reveal All command and you will have boundaries that are now 1:1
6) Delete that Second Layer
7) Add white Layer at bottom
8) Merge back to single Layer
9) Save As file

Good suggestion John, I love the rotating a duped temp layer by 90° trick too, until I learnt that I thought that scripting was the only way.

When I built an action for this, I also wanted to cover flattened vs. layered images and used conditional actions.

2025-02-19_12-27-45.png
 
Thanks everyone. I also have revised this script to not work with a background layer, but rather just ensure the canvas color was set to white. Just also sharing so we all have options :)


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 the canvas to make it square (Photoshop will fill the empty space with the current background color)
            doc.resizeCanvas(newSize, newSize, AnchorPosition.MIDDLECENTER);

            // 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.");
}
 

Back
Top