Skip to main content
Known Participant
December 18, 2019
Answered

Batch process groups of files based on numbering

  • December 18, 2019
  • 3 replies
  • 4469 views

Hello,

so here is the problem:

I have set of files:

OldImage0001.jpg, OldImage0002.jpg etc. going to OldImage0252.jpg lets say (252 files)

Then I have

Mask0001.jpg, Mask0002.jpg etc. going to Mask0252.jpg

And finally

NewImage0001.jpg, NewImage0002.jpg etc. going to NewImage0252.jpg lets say (252 files)

 

How do I do this:
Open OldImage0001.jpg put over NewImage0001.jpg and mask it with Mask0001.jpg - merge everything and produce FinishedImage0001.jpg.
Then let this to be done over all 252 image groups?


Thank you.

This topic has been closed for replies.
Correct answer Stephen Marsh

I have some ideas and might be able to help, however, this may be a little advanced for my scripting level.

 

can you provide download links to 3 sets of images?


Here is my take, it could be optimized and have error checking added, however it works as expected in my tests using three random sets of image sequence numbers.

 

 

 

 

/* Combine Old - New - Mask Images to JPEG.jsx

//community.adobe.com/t5/photoshop/batch-process-groups-of-files-based-on-numbering/td-p/10809093

NOTE: There is no error checking, the 3 input folders must all contain the same quantity of images! 

Input files are expected to have filenames such as: 
OldImage0001.jpg, OldImage0002.jpg etc.
Mask0001.jpg, Mask0002.jpg etc.
NewImage0001.jpg, NewImage0002.jpg etc.

Output files will be modified from the input name:
FinishedImage0001.jpg, FinishedImage0002.jpg etc.

The mask data is based off the RGB composite channel as there was no other info to go by...

It is also assumed that the old/new/mask files all have the same width/height/resolution.

*/

// Prompt for input and output folders
var oldImages = Folder.selectDialog('Select the old images folder...', '~/desktop/'); // select the old images folder
var newImages = Folder.selectDialog('Select the new images folder...', '~/desktop/'); // select the new images folder
var maskImages = Folder.selectDialog('Select the mask images folder...', '~/desktop/'); // select the mask images folder 
var outFolder = Folder.selectDialog('Select the save/output folder...', '~/desktop/'); // select the output images folder

// JPG Search Mask
var searchMask = '*.jpg';
var fileList1 = oldImages.getFiles(searchMask);
var fileList2 = newImages.getFiles(searchMask);
var fileList3 = maskImages.getFiles(searchMask);

// Force an alpha-numeric sort
fileList1.sort();
fileList2.sort();
fileList3.sort();

// JPEG Options
var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = 12; // Quality Level
jpegOptions.embedColorProfile = true; // or false
jpegOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpegOptions.matte = MatteType.NONE;

// Input Loop
for (var i = 0; i < fileList1.length; i++) {
    var doc1 = open(fileList1[i]);
    var doc2 = open(fileList2[i]);
    var doc3 = open(fileList3[i]);

    // Start - Doing stuff to open files

    var maskDoc = app.documents[2];
    var newDoc = app.documents[1];
    var oldDoc = app.documents[0];

    app.activeDocument.selection.selectAll();
    app.activeDocument.selection.copy(); // Copy the third image 'mask'
    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
    app.activeDocument = oldDoc; // Target first image 'old'
    app.activeDocument.paste();

    app.activeDocument = newDoc; // Target second image 'new'
    app.activeDocument.selection.selectAll();
    app.activeDocument.selection.copy(); // Copy the third image 'mask'
    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
    app.activeDocument = oldDoc; // Target first image 'old'
    app.activeDocument.paste();

    // Add layer mask (Clean SL)
    makeLayermask();

    function makeLayermask() {
        var c2t = function (s) {
            return app.charIDToTypeID(s);
        };
        var s2t = function (s) {
            return app.stringIDToTypeID(s);
        };
        var descriptor = new ActionDescriptor();
        var reference = new ActionReference();
        descriptor.putClass(s2t("new"), s2t("channel"));
        reference.putEnumerated(s2t("channel"), s2t("channel"), s2t("mask"));
        descriptor.putReference(s2t("at"), reference);
        descriptor.putEnumerated(s2t("using"), c2t("UsrM"), s2t("revealAll"));
        executeAction(s2t("make"), descriptor, DialogModes.NO);
    }

    // Apply image mask layer (Clean SL)
    applyImage(true);

    function applyImage(preserveTransparency) {
        var c2t = function (s) {
            return app.charIDToTypeID(s);
        };
        var s2t = function (s) {
            return app.stringIDToTypeID(s);
        };
        var descriptor = new ActionDescriptor();
        var descriptor2 = new ActionDescriptor();
        var reference = new ActionReference();
        reference.putEnumerated(s2t("channel"), s2t("channel"), s2t("RGB"));
        reference.putName(s2t("layer"), "Layer 1");
        descriptor2.putReference(s2t("to"), reference);
        descriptor2.putBoolean(s2t("preserveTransparency"), preserveTransparency);
        descriptor.putObject(s2t("with"), c2t("Clcl"), descriptor2);
        executeAction(s2t("applyImageEvent"), descriptor, DialogModes.NO);
    }

    app.activeDocument.activeLayer = app.activeDocument.layers.getByName("Layer 1");
    app.activeDocument.activeLayer.remove();

    // Finish - Doing stuff to open files

    // Save JPEG
    var docName = app.activeDocument.name.replace(/^Old/i, '');
    app.activeDocument.flatten();
    app.activeDocument.saveAs(new File(outFolder + '/' + 'Finished' + docName.split('.')[0] + '.jpg'), jpegOptions);
    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}

alert('Script completed!');

 

 

 

 

 

 

3 replies

Stephen Marsh
Community Expert
Community Expert
December 22, 2019

The first question that pops into mind is – are the source images in 3 separate folders, or in a single folder?

JakubCechAuthor
Known Participant
December 22, 2019

Hello,

thank you.

will try scriptListener but would be cool to see example even here 🙂

I can put files in separate folder - no problem.

do you know how to do it then?

Stephen Marsh
Community Expert
Community Expert
December 23, 2019

I have some ideas and might be able to help, however, this may be a little advanced for my scripting level.

 

can you provide download links to 3 sets of images?

JJMack
Community Expert
Community Expert
December 18, 2019

Jpeg files have a single background layer that does not support transparency so can not have a mask. If you were to paste a new image in the open old image  jpeg background layer from a second open new image jpeg a new layer would be added over the background layer it would contain no transparency. Depending on its size it would cover all or part of the document canvas. So all or part if the old image will be covered.  The second layer will be a normal raster image layer that can be masked.  If you mask that layer using the third jpeg  image luminosity  part or the second image layer will contain transparency which will reveal parts of the  old image below it.  Saving this as a jpeg you will have a composite of the old and new image. Is this what you want to do?

JJMack
Chuck Uebele
Community Expert
Community Expert
December 18, 2019

You would have to do that with a script. Using the plugin scriptListener, you can get much of the code that you need. There have been other threads that want to do things somewhat similar, in which you can find some rough examples.