10 Replies Latest reply on Jun 16, 2015 8:22 AM by c.pfaffenbichler

    Automation of picture overlay/screen

    mb26

      Hi! I have many pairs of pictures that need to be overlaid i.e. a1 with a2, b1 with b2 etc. I would like to import the pairs of pictures and then overlay them. Once overlaid, I would also like to reduce the brightness of the top layer.

       

      I have over 4000 pairs of these pictures and I wish automate it. I have some basic programming experience but I'm unsure where to start. Any advice/guidance would be appreciated.

        • 1. Re: Automation of picture overlay/screen
          c.pfaffenbichler Level 9

          Are the images flat in themselves?

          • 2. Re: Automation of picture overlay/screen
            mb26 Level 1

            Yes they are all flat images saved as TIFF files and are of the same size.

            • 3. Re: Automation of picture overlay/screen
              c.pfaffenbichler Level 9

              Not sure what you meant by

              reduce the brightness of the top layer

              exactly, this would reduce its opacity.

              // 2015, use it at your own risk;
              #target "photoshop-70.032"
              // dialog for folder-selection;
              var theFolder = Folder.selectDialog ("select a folder containing the images");
              var theFolderTarget = Folder.selectDialog ("select a folder to save the combined images to");
              if (theFolder && theFolderTarget) {
                var theFilesOne = theFolder.getFiles(checkFor);
              // create the psd-options;
                psdOpts = new PhotoshopSaveOptions();
                psdOpts.embedColorProfile = true;
                psdOpts.alphaChannels = false;
                psdOpts.layers = true;
                psdOpts.spotColors = true;
              // run through the files;
                for (var a  = 0; a < theFilesOne.length; a++) {
                var theFile = File(theFilesOne[a]);
                var theName = File(theFile).name.match(/(.*)\.[^\.]+$/)[1];
              // get number and add 1;
                var theNumber = theName.match(new RegExp('[0-9]{1,7}$','i'));
                if (String(theNumber/2).indexOf(".") != -1) {
                var theOtherNumber = Number(theNumber)+1;
                while (String(theOtherNumber).length < String(theNumber).length) {
                theOtherNumber = String("0"+String(theOtherNumber));
                };
                var theOtherFile = File(String(theFilesOne[a]).replace(theNumber, theOtherNumber));
              // if corresponding file exists;
                if (theOtherFile.exists == true) {
                var thisFile = app.open(theFile);
                thisFile.activeLayer = thisFile.layers[0];
              // place foreground-image;
                var theSO = placeScaleRotateFile (theOtherFile, 0, 0, 100, 100, 0);
                theSO.opacity = 50;
              // save the combined files;
                thisFile.saveAs(new File (theFolderTarget + "/" + theName + "_comb.psd"), psdOpts)
                thisFile.close(SaveOptions.DONOTSAVECHANGES);
                }
                }
                }
                };
              ////////////////////////////////////
              ////// check for psd, tif or jpg //////
              function checkFor (theFile) {
                if (theFile.name.slice(-4) == ".psd" || theFile.name.slice(-4) == ".tif" || theFile.name.slice(-4) == ".jpg") {return true}
                else {return false}
                };
              ////// place //////
              function placeScaleRotateFile (file, xOffset, yOffset, theXScale, theYScale, theAngle) {
              // =======================================================
              var idPlc = charIDToTypeID( "Plc " );
                  var desc5 = new ActionDescriptor();
                  var idnull = charIDToTypeID( "null" );
                  desc5.putPath( idnull, new File( file ) );
                  var idFTcs = charIDToTypeID( "FTcs" );
                  var idQCSt = charIDToTypeID( "QCSt" );
                  var idQcsa = charIDToTypeID( "Qcsa" );
                  desc5.putEnumerated( idFTcs, idQCSt, idQcsa );
                  var idOfst = charIDToTypeID( "Ofst" );
                      var desc6 = new ActionDescriptor();
                      var idHrzn = charIDToTypeID( "Hrzn" );
                      var idPxl = charIDToTypeID( "#Pxl" );
                      desc6.putUnitDouble( idHrzn, idPxl, xOffset );
                      var idVrtc = charIDToTypeID( "Vrtc" );
                      var idPxl = charIDToTypeID( "#Pxl" );
                      desc6.putUnitDouble( idVrtc, idPxl, yOffset );
                  var idOfst = charIDToTypeID( "Ofst" );
                  desc5.putObject( idOfst, idOfst, desc6 );
                  var idWdth = charIDToTypeID( "Wdth" );
                  var idPrc = charIDToTypeID( "#Prc" );
                  desc5.putUnitDouble( idWdth, idPrc, theYScale );
                  var idHght = charIDToTypeID( "Hght" );
                  var idPrc = charIDToTypeID( "#Prc" );
                  desc5.putUnitDouble( idHght, idPrc, theXScale );
                  var idAngl = charIDToTypeID( "Angl" );
                  var idAng = charIDToTypeID( "#Ang" );
                  desc5.putUnitDouble( idAngl, idAng,theAngle );
              executeAction( idPlc, desc5, DialogModes.NO );
              return app.activeDocument.activeLayer;
              };
              
              
              • 4. Re: Automation of picture overlay/screen
                mb26 Level 1

                Hey! Thanks for your reply, much appreciated. The script worked perfectly, just have a couple of questions before I start using it for all my pictures.

                1. Each folder has 100-250 pictures, whats the best way to label each pair? Would it work if I name them 1.1, 1.2 all the way to 200.1, 200.2 etc?

                2. Is there a way for it to loop through all folders in a particular folder? For example, I tell it to go to a folder A which contains 20 folders, can it go through every file in each folder, combine the pictures, then place the combined file within a sub-folder there (I can remove the output folder selection)?

                Any guidance would be great - I would be willing to learn and have a crack at modifying the coding.

                • 5. Re: Automation of picture overlay/screen
                  c.pfaffenbichler Level 9

                  One should not use "." in a filename except to signify the file extension (like ".psd").

                   

                  2. Is there a way for it to loop through all folders in a particular folder? For example, I tell it to go to a folder A which contains 20 folders, can it go through every file in each folder, combine the pictures, then place the combined file within a sub-folder there (I can remove the output folder selection)?

                  One could amend the function "checkFor" accordingly and have the Script create Folders that are then used as "theFolderTarget".

                  • 6. Re: Automation of picture overlay/screen
                    mb26 Level 1

                    Thank you for your help.

                     

                    Over the last few weeks, I have been trying to learn coding for photoshop and though I have managed to learn a bit, I wasn’t able edit the code successfully.

                     

                    Would anyone be able to assist me in modifying the above code so that it is able to overlay 2 or 3 images, depending whether the third file existed in the folder. That is, if the images are named:

                    -       001_1, 001_2, 001_3 and

                    -       002_1, 002_2

                    -       003_1, 003_2, 003_3

                     

                    Then it would merge the first 3, and then the next 2, and then the next 3, etc. I can follow any naming convention required (some folders have 100+ files).

                     

                    Any help is much appreciated.

                     

                    Thanks

                    • 7. Re: Automation of picture overlay/screen
                      c.pfaffenbichler Level 9

                      I suppose this may be a lot more convoluted than the result would require but nonetheless maybe it helps.

                      The result is an Array of Arrays – these Arrays contain a String (the filename minus the number and suffix) and an Array of the files that "fit this name".

                      You could then run the "merging"-operation on the files in those Arrays.

                      // 2015, use it at your own risk;
                      // dialog for folder-selection;
                      var theFolder = Folder.selectDialog ("select a folder containing the images");
                      if (theFolder) {
                      var theFilesOne = theFolder.getFiles(checkFor);
                      // create the psd-options;
                      psdOpts = new PhotoshopSaveOptions();
                      psdOpts.embedColorProfile = true;
                      psdOpts.alphaChannels = false;
                      psdOpts.layers = true;
                      psdOpts.spotColors = true;
                      // run through the files;
                      var theArray = new Array;
                      for (var a  = 0; a < theFilesOne.length; a++) {
                      var theFile = theFilesOne[a];
                      var theName = File(theFile).name.match(/(.*)\.[^\.]+$/)[1];
                      var theRegExp = new RegExp('[0-9]{1,7}$','i');
                      var theNumber = theName.match(theRegExp);
                      var thePrefix = theName.replace(theRegExp, "");
                      var inArray = false;
                      for (var b = 0; b < theArray.length; b++) {
                      if (theArray[b][0] == thePrefix) {
                      theArray[b][1].push(theFile)
                      inArray = true;
                      }
                      else {}
                      };
                      if (inArray == false || a == 0) {theArray.push([thePrefix, [theFile]])};
                      }
                      for (var c = 0; c < theArray.length; c++) (alert (theArray[c][1].join("\n\n")))
                      };
                      ////////////////////////////////////
                      ////// check for psd, tif or jpg //////
                      function checkFor (theFile) {
                        if (theFile.name.slice(-4) == ".psd" || theFile.name.slice(-4) == ".tif" || theFile.name.slice(-4) == ".jpg") {return true}
                        else {return false}
                        };
                      
                      • 8. Re: Automation of picture overlay/screen
                        c.pfaffenbichler Level 9

                        To clarify previous code this shows an example of a screenshot of a folder and the resulting Array from above code run with this Folder selected.

                        combineMultipleImagesScr.jpg

                        • 9. Re: Automation of picture overlay/screen
                          mb26 Level 1

                          Hey, sorry to bother you again.

                           

                          I’ve tried to understand the code as best as I can, but am confused about where I should open the files, specifically, for the code you had previously:

                           

                           var thisFile = app.open(theFile);  
                           thisFile.activeLayer = thisFile.layers[0];
                           var theSO = placeScaleRotateFile (theOtherFile, 0, 0, 100, 100, 0); 
                           thisFile.saveAs(new File (theFolderTarget + "/Combined_" + theName + ".jpeg"), psdOpts) 
                          

                           

                          Would I also have to edit the placeScaleRotateFile function?

                           

                          As I am running out of time, I might have to revert to using the original script for 2 files, and then re-running it again with the combined picture and the third file. If so, what would be the best way to name the files in the folder?

                           

                          My current file structure for the 3 file folder is as below.

                          Screen Shot 2015-06-16 at 11.47.29 pm.png

                           

                          Thanks again.

                          • 10. Re: Automation of picture overlay/screen
                            c.pfaffenbichler Level 9

                            Open the first file in the array, then place the rest with a for-clause starting with 1.