4 Replies Latest reply on Jun 16, 2015 1:47 PM by Chris Jiggins

    Combining paired JPG files into layers

    Chris Jiggins

      I have a folder with around 1600 images. I would like to combine paired images from this folder into layers in a photoshop document. The files are currently named something like this: XXX-1234_123 and XXX-1234_124 so the name before the underscore is shared between the images that need to be combined and the final number is unique to each image.


      Just to be clear:





      So I want to combine the first two into one file, and then the second two etc.


      I have never used scripting in photoshop, so I am a bit lost as to how to go about this, although it looks like it shouldn't be too difficult. Does anyone have a script that does something similar that I could use, with a bit of advice? Once I have the photoshop files I'm fine with using actions and batching to carry out subsequent modifications to the layers.


      The files are images of butterfly specimens - upper and undersides.


      Any help much appreciated

        • 1. Re: Combining paired JPG files into layers
          michelr31372089 Level 2

          Hi Chris,

          Excuse my poor English. I can help you, but I have three questions to ask you :

          - when you write that the name before the underscore is shared between the images that need to be combined, does it mean then there are only 2 files in your list beginning by XXX-1235 ?

          - if you had exactly 1600 photos in your folder, can we suppose that the fisrt binome would end by _1.jpg and _2.jpg and that the last combination identifiers would be _1599.jpg and _1600.jpg ?

          - when you will save, for instance, the first combined document of your example, is XXX-1234.psd the fileName to save ?

          • 2. Re: Combining paired JPG files into layers
            Chuck Uebele Adobe Community Professional & MVP

            Much of this you can do with Scriptlistener: create the file and layout of how you want the images set up. If you don't have extendscript tool kit download it. It used to load with PS, but now you have to download it from the creative cloud application manager application. While what you want to do is not that difficult, it would require a bit of coding, and most of us just don't have the time to write every script request, but were happy to help point you in the right direction. What you basically want to do it define a folder via script where all your source files are. You can hard code this in or create code that pops up the folder dialog box. Hard coding is just something like:


            var myFile = new Folder('myPath');


            Next you need to get what files you want. and with the format you set up, you can get them with a search mask and counter and the getFiles commant.


            var counter = 10001
            var counterString = counter..substr (1, 4)//This keeps zeros in front of your number
            var searchMask = '*-' + counterString +'_*' //gets just the numbering of the pairs


            Then you need to make a loop statement for going though all your files and use the searchMask to find each pair, then afterwards bump up the counter by one for the next set,  but it's late and that's all I can write at the moment.

            1 person found this helpful
            • 3. Re: Combining paired JPG files into layers
              michelr31372089 Level 2

              You can try this script. We shall adapt if necessary :


              // MAC Finder or WINDOWS Explorer  double clic enabled 
              #target photoshop
              // Variables
              var docRefSup = "";
              var docRefInf = "";
              var layerRef = "";
              var psd_Options  = new PhotoshopSaveOptions(); // [PhotoshopSaveOptions] 
              psd_Options.layers  = true; // preserve layers
              psd_Options.embedColorProfile = true; // preserve color profile
              psd_Options.annotations = true; // preserve annotations
              psd_Options.alphaChannels = true; // preserve Alpha channels
              psd_Options.spotColors = true; // preserve Inks
              // Selection the INPUT folder and put the images in an Array
              var inputFolder = Folder.selectDialog("Select the butterflies folder : ");
              var outputFolder = Folder(inputFolder + "/save");
              // Create a directory as OUTPUT folder if it does not exist
              if (!outputFolder.exists) outputFolder.create();
              var fileList = inputFolder.getFiles(/\.(jpg|jpeg|png|psd|)$/i);
              // Main program
              for (var i= 0;i<fileList.length;i++) 
                  docRefSup = open(fileList[i]);
                  docRefInf = open(fileList[i+1]);
                  app.activeDocument = app.documents[0];
                  layerRef = app.activeDocument.activeLayer;
                  app.activeDocument = app.documents[1];
                  app.activeDocument.layers[0].name = "Sup";
                  app.activeDocument.layers[1].name = "Inf";
                  var fileName  = app.activeDocument.name;
                  fileName  = fileName.substring( 0, fileName.indexOf('_') ) + ".psd" ;  
                  // Save the combined file
                  app.activeDocument.saveAs( File(outputFolder + '/' + fileName), psd_Options, true );
              // END
              • 4. Re: Combining paired JPG files into layers
                Chris Jiggins Level 1

                Wow, thanks that seems to work! I just tried it so far on a few images - now need to do some tidying up before running the whole set.


                Many thanks to all of you for prompt response!