Skip to main content
19304886
Participant
June 20, 2020
Question

How to I batch automate combining files as layers

  • June 20, 2020
  • 2 replies
  • 2979 views

I have 10k odd files that have a pair (so 20k total) image which is imageName.bmp and imageName_a.bmp that I would like to combine into one .psd, then perform some Photoshop Actions on, then save both layers independently to two files (one with the _a one without). Also some files don't have a _a counterpart.

What's the best way to automate this? I'm happy to learn about scripting and I already understand PS Actions. Cheers 🙂

This topic has been closed for replies.

2 replies

Stephen Marsh
Community Expert
Community Expert
June 23, 2020

OK, this is far from perfect, however, it is the best that I can currently do (I still consider myself a beginner).

 

You should temporarily move the files that don't have a matching _a.bmp alpha file to a temporary folder so that they are not processed.

 

Hope this helps.

 

WARNINGS & NOTES FOR USE:

 

  •  Use at your own risk!!!
  •  There must be multiple pairs of files, not an odd number of input files, there is no error checking
  •  Work on copies of the original files until you are happy with the results
  •  The original .bmp file will be overwritten, I did I mention to use at your own risk and to work on copies!
  •  The code could be adjusted to save the adjusted file with a new name rather than overwrite the original (there are 10,000+ files to consider)
  •  The alpha _a.bmp files will be closed without saving changes
  •  Only the top-level folder will be processed, sub-directories must be processed separately
  •  A selection will be loaded from the RGB composite channel of the alpha data
  •  An action will then be run to complete the processing using the selection
  •  Change the action name from Molten Lead to your name
  •  Change the action set name from Default Actions to your name

 

 

// Use at your own risk!!!
// There must be multiple pairs of files, not an odd number of input files, there is no error checking
// Work on copies of the original files until you are happy with the results
// The original .bmp file will be overwritten
// The alpha _a.bmp files will be closed without saving changes
// Only the top-level folder will be processed, sub-directories must be processed separately
// A selection will be loaded from the RGB composite channel of the alpha data
// An action will then be run to complete the processing using the selection
// Change the action name from Molten Lead to your name
// Change the action set name from Default Actions to your name

#target photoshop

// Capture and suppress dialogs during batch
var savedDisplayDialogs = app.displayDialogs;
app.displayDialogs = DialogModes.NO;

main();

function main() {

    inputFolder = Folder.selectDialog("Please select the input folder");
    if (inputFolder == null) return;
    // Limit the file input
    var fileList = inputFolder.getFiles(/\.(bmp)$/i);
    // Force alpha-numeric list sort
    fileList.sort();

    while (fileList.length) {
        // Digit 2 for two files
        for (var a = 0; a < 2; a++) {
            try {
                app.open(fileList.pop());
            } catch (e) { }
        }

        //var fileCount = app.documents.length;
        //alert("There are " + fileCount + " files to process");

        doStuff();
    }

    function doStuff() {
        try {
            // Select the alpha document
            app.activeDocument = app.documents[0];
            // Select all
            app.activeDocument.selection.selectAll();
            // Copy selection
            app.activeDocument.selection.copy();
            // Close active document without saving
            app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
            // Paste from clipboard
            app.activeDocument.paste();
            // Load RGB channel as selection
            var idsetd = charIDToTypeID("setd");
            var desc146 = new ActionDescriptor();
            var idnull = charIDToTypeID("null");
            var ref59 = new ActionReference();
            var idChnl = charIDToTypeID("Chnl");
            var idfsel = charIDToTypeID("fsel");
            ref59.putProperty(idChnl, idfsel);
            desc146.putReference(idnull, ref59);
            var idT = charIDToTypeID("T   ");
            var ref60 = new ActionReference();
            var idChnl = charIDToTypeID("Chnl");
            var idChnl = charIDToTypeID("Chnl");
            var idRGB = charIDToTypeID("RGB ");
            ref60.putEnumerated(idChnl, idChnl, idRGB);
            desc146.putReference(idT, ref60);
            executeAction(idsetd, desc146, DialogModes.NO);
            // Remove active layer
            app.activeDocument.activeLayer.remove();

            // Now do something with the selection active to limit the effects...

            /*
            // Brightness +30, Contrast +90, Use Legacy = False
            var idBrgC = charIDToTypeID( "BrgC" );
            var desc71 = new ActionDescriptor();
            var idBrgh = charIDToTypeID( "Brgh" );
            desc71.putInteger( idBrgh, 30 );
            var idCntr = charIDToTypeID( "Cntr" );
            desc71.putInteger( idCntr, 90 );
            var iduseLegacy = stringIDToTypeID( "useLegacy" );
            desc71.putBoolean( iduseLegacy, false );
            executeAction( idBrgC, desc71, DialogModes.NO );
            */

            ///////////// Your action here /////////////
            // Action to run
            var actionName = "Molten Lead";
            // Action set to run
            var actionSet = "Default Actions";
            app.doAction(actionName, actionSet);
            ///////////// Your action here /////////////

            app.activeDocument.flatten();
            app.activeDocument.close(SaveOptions.SAVECHANGES);

        } catch (e) { }
    }
}

// Restore original dialog settings
app.displayDialogs = savedDisplayDialogs;

 

 

 

23rd JUNE 2020

Initial code posted

 

24th JUNE 2020

I have added code for the Brightness/Contrast step to remove the dependency on the action, however, I have commented out the code and left the action in there for now, just in case the action is doing more than this single step...

 

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

 

Stephen Marsh
Community Expert
Community Expert
June 27, 2020

So, how did you go?

Stephen Marsh
Community Expert
Community Expert
June 21, 2020

Yes, this is possible via scripting, there are numerous examples for batch processing file pairs in the forums.

 

 I have to ask why???

 

 If you are going to save out each layer as a separate layer, why bother combining them as pairs?

 

What are you doing with the action on the two layer stack, before saving out separate files again? 

 

P.S. Do you also wish to save the layered PSD as well as the separate layers?

19304886
19304886Author
Participant
June 21, 2020

Yeah "why" is a fair question. Basically I need to up the brightness and contrast on the first image, according to a selection based off the second. The _a image is an alpha channel pair for the main image, I don't want to affect certain parts of the image with the brightness and contrast changes.

Stephen Marsh
Community Expert
Community Expert
June 21, 2020

That makes sense.

 

I am testing on a Mac, Mac and Windows can sort batch file input differently.

 

Can you please provide 3 sets of real filenames, so 6 in total.