• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Photoshop script for image stacking

Community Beginner ,
Apr 26, 2022 Apr 26, 2022

Copy link to clipboard

Copied

Hi there

I am seeking a solution to streamline part of my work flow. I want to be able to automate the following steps but am unsure how to get there:

1. Load files into stack - load a bunch of photos into a photoshop file, pretty simple - I would do this manually.

Hoping to script this part:

2. Select the first and second layer and duplicate them into a new photoshop file.

3. Repeat step 2 for the rest of the images in the original stack.

That's basically it, but when I tried to record an action for this process, the variable amount of photos per photoshoot made it so that the action either didn't run enough times or ran too many times creating many duplicates.

Any ideas?

TOPICS
Actions and scripting , macOS

Views

820

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 26, 2022 Apr 26, 2022

@andrewh85004476 wrote:

Hi Stephen thanks for the reply.

It will always be a multiple of 2 as the images are paired for further editing. 1 flash shot and one non-flash of the same scene.

I don't see why selecting a folder of 10 would not work - in fact it would probably be more efficient that way


 

Try this script:

 

/* 

Stephen Marsh
20th August 2021 Version

This script requires input files from a single folder to be alpha/numeric sorting in order to stack in the correct
set quantity. 

Examp
...

Votes

Translate

Translate
Adobe
Community Expert ,
Apr 26, 2022 Apr 26, 2022

Copy link to clipboard

Copied

Will the layers length always be a multiple of 2?

 

Rather than stacking say 10 files into a single file, then generating another five x2 stack files, would selecting a folder of 10 files and having the script generate five x2 stack files work? Presuming alpha/numeric filename sorting...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 26, 2022 Apr 26, 2022

Copy link to clipboard

Copied

Hi Stephen thanks for the reply.

It will always be a multiple of 2 as the images are paired for further editing. 1 flash shot and one non-flash of the same scene.

I don't see why selecting a folder of 10 would not work - in fact it would probably be more efficient that way

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 26, 2022 Apr 26, 2022

Copy link to clipboard

Copied


@andrewh85004476 wrote:

Hi Stephen thanks for the reply.

It will always be a multiple of 2 as the images are paired for further editing. 1 flash shot and one non-flash of the same scene.

I don't see why selecting a folder of 10 would not work - in fact it would probably be more efficient that way


 

Try this script:

 

/* 

Stephen Marsh
20th August 2021 Version

This script requires input files from a single folder to be alpha/numeric sorting in order to stack in the correct
set quantity. 

Example: File-01.jpg File-02.jpg etc, FileA1.tif FileA2.tif etc, File1a.tif File1b.tif etc.

A minimum of 2 or more files per stack is required. The quantity of input files must be evenly divisible by 2.

A named action set and action can be set on line 89 to "do something" with the stacked layers, or further code could be added.

*/

#target photoshop

// app.documents.length === 0
if (!app.documents.length) {

    try {

        // Save and disable dialogs
        var restoreDialogMode = app.displayDialogs;
        app.displayDialogs = DialogModes.NO;

        // Main script function
        (function () {

            // Select the input folder
            var inputFolder = Folder.selectDialog('Please select the folder with files to process');
            if (inputFolder === null) return;

            // Limit the file format input, add or remove as required
            var fileList = inputFolder.getFiles(/\.(png|jpg|jpeg|tif|tiff|psd|psb)$/i);

            // Force alpha-numeric list sort
            // Use .reverse() for the first filename in the merged file
            // Remove .reverse() for the last filename in the merged file
            fileList.sort().reverse();

            var setQty = 2;

            // Validate that the file list is not empty
            var inputCount = fileList.length;
            var cancelScript1 = (inputCount === 0);
            if (cancelScript1 === true) {
                alert('Zero input files found, script cancelled!');
                return;
            }
            // Validate the input count vs. output count - Thanks to Kukurykus for the advice to test using % modulus
            var cancelScript2 = !(inputCount % setQty);
            alert(inputCount + ' input files stacked into sets of ' + setQty + ' will produce ' + inputCount / setQty + ' output files.');
            // Test if false, then terminate the script
            if (cancelScript2 === false) {
                alert('Script cancelled as the quantity of input files are not evenly divisible by the set quantity.');
                return;
            }

            // Select the output folder
            var outputFolder = Folder.selectDialog("Please select the folder to save to");
            if (outputFolder === null) {
                alert('Script cancelled!');
                return;
            }

            // Loop through and open the file sets
            while (fileList.length) {
                // Sets of N quantity files
                for (var a = 0; a < setQty; a++) {
                    try {
                        app.open(fileList.pop());
                    } catch (e) { }
                }

                // Set the base doc layer name
                app.activeDocument = documents[0];
                docNameToLayerName();

                // Stack all open docs to the base doc
                while (app.documents.length > 1) {
                    app.activeDocument = documents[1];
                    docNameToLayerName();
                    app.activeDocument.activeLayer.duplicate(documents[0]);
                    app.activeDocument = documents[0];

                    ////////////////////////////////// Start doing stuff //////////////////////////////////
                    // app.doAction("My Action", "My Action Set Folder");
                    ////////////////////////////////// Finish doing stuff //////////////////////////////////

                    app.documents[1].close(SaveOptions.DONOTSAVECHANGES);
                }

                // Delete XMP metadata to reduce final file size of output files
                removeXMP();

                // Save name + suffix & save path
                var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
                var saveFile = File(outputFolder + '/' + Name + '_x' + setQty + '-Sets' + '.psd');

                // Call the save function
                savePSD(saveFile);

                // Close all open files without saving
                while (app.documents.length) {
                    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                }

                // Functions

                function savePSD(saveFile) {
                    psdSaveOptions = new PhotoshopSaveOptions();
                    psdSaveOptions.embedColorProfile = true;
                    psdSaveOptions.alphaChannels = true;
                    psdSaveOptions.layers = true;
                    psdSaveOptions.annotations = true;
                    psdSaveOptions.spotColors = true;
                    // Save as
                    app.activeDocument.saveAs(saveFile, psdSaveOptions, true, Extension.LOWERCASE);
                }

                function docNameToLayerName() {
                    var layerName = app.activeDocument.name.replace(/\.[^\.]+$/, '');
                    app.activeDocument.activeLayer.name = layerName;
                }

                function removeXMP() {
                    if (!documents.length) return;
                    if (ExternalObject.AdobeXMPScript == undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
                    var xmp = new XMPMeta(activeDocument.xmpMetadata.rawData);
                    XMPUtils.removeProperties(xmp, "", "", XMPConst.REMOVE_ALL_PROPERTIES);
                    app.activeDocument.xmpMetadata.rawData = xmp.serialize();
                }

            }

            // Restore saved dialogs
            app.displayDialogs = restoreDialogMode;

            // End of script notification
            app.beep();
            alert('Script completed!' + '\n' + 'Combined files saved to:' + '\n' + outputFolder.fsName);

            // Open the output folder in the Finder or Explorer
            // outputFolder.execute();

        }());

    } catch (e) {

        // Restore saved dialogs
        app.displayDialogs = restoreDialogMode;
        alert("If you see this message, something went wrong!");

    }
}

else {

    alert('Stack Into Sets of 2:' + '\n' + 'Please close all open documents before running this script!');

}

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 26, 2022 Apr 26, 2022

Copy link to clipboard

Copied

Hey Stephen - thanks again for your reply. The script is almost perfect thanks so much. It reverses the order of the pairs though, is there a way to modify the script to make sure they stay in order (lower number as top layer)?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 26, 2022 Apr 26, 2022

Copy link to clipboard

Copied

Sure, it is mentioned in the code comments...

 

Change:

fileList.sort().reverse();


To:

fileList.sort();

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 26, 2022 Apr 26, 2022

Copy link to clipboard

Copied

I must have missed that. You're the best Stephen thanks again!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 26, 2022 Apr 26, 2022

Copy link to clipboard

Copied

No worries, do you need to automate once stacked, save to other file formats etc?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 26, 2022 Apr 26, 2022

Copy link to clipboard

Copied

Once stacked I have to run a batch action to all of the stacks and then manually edit from there. I see there is a spot for additional automation in the script but I don't have any idea how to convert the action into txt format. After manual edits I would just go scripts -> Image Processor and batch export the files.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 26, 2022 Apr 26, 2022

Copy link to clipboard

Copied

All you need to do is remove the leading comment // double slashes and refer to the action and action set by exact spelling:

 

atn.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 26, 2022 Apr 26, 2022

Copy link to clipboard

Copied

You're an absolute golden god. Thank you.

One last request if I may: Is it possible to modify the script so that the files remain open in photoshop for further editing? Having them all close down and then opening all of them up again seems a bit roundabout but will certainly work if necessary.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 26, 2022 Apr 26, 2022

Copy link to clipboard

Copied

LATEST

The script uses the two open files for stacking, so would need a rewrite to work with other files open.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines