11 Replies Latest reply on Sep 26, 2012 3:47 PM by Michael L Hale

    Automatic Image Compilation

    BMorden

      Hello All,

       

      I am hoping this great community can help me out.

       

      I am looking to take two images and automatically compile them in to one.  Seems easy but there is a catch, I guess there is always a catch.

       

      Image A is the same each time.  It is a picture of a person in a 4x6 aspect ratio.

       

      Image B changes each time.  This image also has a predetermined location that is transparent where I want Image A to be resized to and placed automatically.

       

      Step as I see them

       

      Take image B and determine the size and location of the transparency

      Resize image A to be the size determined

      Create a canvas to the overall size of image B

      Move image A to the location of the transparency on the new canvas

      Place image B on on the new canvas thus being on top of image A

      Save new Canvas

       

      Is this possibly to script so I can batch do 350 or so of these?

       

      Thanks in advance.

        • 1. Re: Automatic Image Compilation
          Michael L Hale Level 5

          I am trying to understand this workflow. As I understand what you outlined you have a person image which you want to combine with multiple other images( scenes maybe? ). If that is correct I have several questions.

           

          1. Are the B images unique to the person image? Or is B setup to work with other person images?.

          2. Is there only one person image? Or do you need to do this with muptiple person images as well( each with a set of B images )?

          3. If this needs to work with multiple person images is each person in the same position/size in the image?

          4. Are there 350 B images? Or does that 350 batch run contain a mix of A and sets of B?

          • 2. Re: Automatic Image Compilation
            JJMack Most Valuable Participant

            BMorden wrote:

             

             

            I am looking to take two images and automatically compile them in to one.  Seems easy but there is a catch, I guess there is always a catch.

             

            Image A is the same each time.  It is a picture of a person in a 4x6 aspect ratio.

             

            Image B changes each time.  This image also has a predetermined location that is transparent where I want Image A to be resized to and placed automatically.

            I think we more information.   Are Images files?

            Like is image A and image file if so what file format. 

             

            Are images of type B files if so are they PSD or PNG files since they have transparency. 

             

            You want to batch it there are many type A images and many type B  images there needs be some process perhaps has two input folders one for Image A types and one for type b images.  The main routine would get a type A file name then need to be able to open the type b files for that particular type A file.

             

            Once a type B image is open the layers transparency can be selected and inverted to get the transparent area. This could be saved as an Alpha chanel and used to position the place file A smart object layer resize to fit the transparent area and masked with the alpha channel to  do a virtual crop of the 4x6 image to the aspect ration of the transparent area if the area does not have a 2:3 aspect ratio.

            • 3. Re: Automatic Image Compilation
              BMorden Level 1

              Thanks for the responses. 

               

              I have actively tried to solve this as well and I have come up with a pretty darn good solution so far...I have pulled a bunch of different ideas from others and created some custom scripting too.  This is a PoC to show that it can be done for 1 image.  I just need to clean it up and then figure out to it to run as a batch.  Be nice as this is my first attempt at PhotoShop Scripting

               

               

              layerPixels2Selection = function(fLayer){
                        if(mainPhoto.activeLayer.isBackgroundLayer){
                           return;// background doesn't have a transparency mask
                           }
                        var desc = new ActionDescriptor();
                        var ref = new ActionReference();
                        ref.putProperty( charIDToTypeID( "Chnl" ), charIDToTypeID( "fsel" ) );
                        desc.putReference( charIDToTypeID( "null" ), ref );
                        var ref1 = new ActionReference();
                        ref1.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Chnl" ), charIDToTypeID( "Trsp" ) );
                        desc.putReference( charIDToTypeID( "T   " ), ref1 );
                        executeAction( charIDToTypeID( "setd" ), desc, DialogModes.NO );
                
              }
              
              
              function MoveLayerTo(fLayer,fX,fY) {
              
              
                var Position = fLayer.bounds;
                Position[0] = fX - Position[0];
                Position[1] = fY - Position[1];
              
              
                fLayer.translate(-Position[0],-Position[1]);
              }
              //demo
              
              
              // Save the current preferences
              var startRulerUnits = app.preferences.rulerUnits
              var startTypeUnits = app.preferences.typeUnits
              var startDisplayDialogs = app.displayDialogs
              
              
              // Set Adobe Photoshop CS6 to use pixels and display no dialogs
              app.preferences.rulerUnits = Units.PIXELS
              app.preferences.typeUnits = TypeUnits.PIXELS
              app.displayDialogs = DialogModes.NO
              
              
              var mainPhoto = app.activeDocument;
              
              
              layerPixels2Selection(mainPhoto)
              
              
              
              
              mainPhoto.selection.invert();
              
              
              var mainPhotoWidth = mainPhoto.width;
              var mainPhotoHeight = mainPhoto.height;
              var mainPhotoResolution = mainPhoto.resolution;
              var mainPhotoAspect = mainPhoto.width / mainPhoto.height;
              
              
              var selectionTopLeftXOffset = mainPhoto.selection.bounds[0].as('px')
              var selectionTopLeftYOffset = mainPhoto.selection.bounds[1].as('px')
              
              
              var selectionBottomRightXOffset = mainPhoto.selection.bounds[2].as('px')
              var selectionBottomRightYOffset = mainPhoto.selection.bounds[3].as('px')
              
              
              var selectionWidth = mainPhoto.selection.bounds[2].as('px') -  mainPhoto.selection.bounds[0].as('px');
              var selectionHeight = mainPhoto.selection.bounds[3].as('px') -  mainPhoto.selection.bounds[1].as('px');
              var selectionAspect = selectionWidth / selectionHeight;
              
              
              
              
              //alert (selectionWidth + ", " + selectionHeight);
              
              
              var lifestylePhoto = File("C:/Greeting Cards - Resized/lifestyle.jpg")
              
              
              open (lifestylePhoto)
              
              
              var lifestylePhoto = app.documents.getByName("lifestyle.jpg")
              
              
              var lifestylePhotoAspect = lifestylePhoto.width / lifestylePhoto.height;
              
              
              
              
              if (selectionAspect > lifestylePhotoAspect) {
                        var lifestylePhotoNewWidth = selectionWidth; 
                        var lifestylePhotoNewHeight = selectionWidth / lifestylePhotoAspect;
              } else {
                        var lifestylePhotoNewWidth = selectionHeight * lifestylePhotoAspect;
                        var lifestylePhotoNewHeight = selectionHeight;
              }
              
              
              lifestylePhoto.resizeImage(lifestylePhotoNewWidth, lifestylePhotoNewHeight);
              
              
              lifestylePhoto.selection.selectAll();
              lifestylePhoto.selection.copy();
              
              
              app.documents.add(mainPhotoWidth, mainPhotoHeight, mainPhotoResolution, 'newImage');
              
              
              var newPhoto = app.documents.getByName("newImage");
              
              
              newPhoto.paste();
              
              
              MoveLayerTo(newPhoto.artLayers[0], selectionTopLeftXOffset, selectionTopLeftYOffset);
              
              
              app.activeDocument = mainPhoto;
              mainPhoto.selection.selectAll();
              mainPhoto.selection.copy();
              
              
              app.activeDocument = newPhoto
              newPhoto.paste();
              
              
              // Reset the application preferences
              app.preferences.rulerUnits = startRulerUnits
              app.preferences.typeUnits = startTypeUnits
              app.displayDialogs = startDisplayDialogs
              
              
              • 4. Re: Automatic Image Compilation
                Michael L Hale Level 5

                There are normally lots of ways to do something in Photoshop. Some are better suited to being scripted. I had asked for details about the images because there may be a different way to do this than the approach you outlined and it's hard to know without more details or sample images( which I can understand you might not want to share ).

                 

                Having said that, are you happy with what you have now and just want help running your code on a batch of images? Again there may be better/faster ways to do what your script is doing but it's hard to even test the code you posted without knowing more about the images it uses.

                • 5. Re: Automatic Image Compilation
                  BMorden Level 1

                  So sorry I didn't answer the questions...I totally meant to!

                   

                  1. Are the B images unique to the person image? Or is B setup to work with other person images?.

                       B images are just ramdon templates.  There are a set of B images that are mapped to 1 A image.  Explace 50 B images use the same A image

                  2. Is there only one person image? Or do you need to do this with muptiple person images as well( each with a set of B images )?

                       As said above there are a few A images...maybe 10 or so that correlate to a group of B images

                  3. If this needs to work with multiple person images is each person in the same position/size in the image?

                       No each A image does not go in the B image spot in the same size nor aspect ratio nor position

                  4. Are there 350 B images? Or does that 350 batch run contain a mix of A and sets of B?

                       Roughly 350 B images.

                  B image is a png and A image is a jpg.  Both are 300DPI and the B image is a single layer with a transparency.  I have attached an example

                   

                  Image B

                  .AP-G5353-4x8H.png

                   

                  Image A

                  lifestyle1.jpg

                   

                  Final Image - Created with my script above

                  newImage.jpg

                  • 6. Re: Automatic Image Compilation
                    Michael L Hale Level 5

                    Yes, that helps. I was a manager for a one hour photo place for many years and have scripted a very similar task.

                     

                    Here is how I would script this: first go through all the greeting templates and place the same 'person' image as a smart object. This only needs to be done once to set up the workflow. Make note of the 'person' image pixel dimensions.

                     

                    To create a batch of different cards with the same 'person' image you would:

                     

                    1. Resize the new 'person' image to match the dimensions of the one you used to set up the templates and save/saveAs to create a correct size file.

                    2. Open a greeting template.

                    3. Make the Smart Object layer active.

                    4. Replace the contents of the Smart Object layer with the file from step one.

                    5. SaveAs the template with the new 'person' image.

                    6. Close the template without saving.

                    7. Repeat steps 2-6 until you have processed every template.

                     

                    I think this would be much faster once the templates are created. You only need to resize one image( if needed ) and everything else is handled when you replace the SO contents.

                    • 7. Re: Automatic Image Compilation
                      BMorden Level 1

                      The problem comes down to the fact that the Image B transparency is in a different location and is a different size for each one.

                       

                      Also I don't want to do these by hand.

                      • 8. Re: Automatic Image Compilation
                        Michael L Hale Level 5

                        The problem comes down to the fact that the Image B transparency is in a different location and is a different size for each one.

                        I understood that part. When I did this I couldn't even count on the transparent areas being rectangles. Most were but some were ovals or odd shapes.

                         

                        Also I don't want to do these by hand.

                         

                        Far enough. I guess my next question is the same as before - are you happy with what you have and just want help adapting it to handle a batch of images?

                         

                        If so are the template images in the same folder? Do you want the script to expect the 'person' image to already be open? Will the 'person' image always have the same name or be the active document?

                        • 9. Re: Automatic Image Compilation
                          BMorden Level 1

                          Sure helping turn this in to a batch would be most useful.

                           

                          All templates are not in the same folder and the person images could be the same if it makes batching it easier.

                          • 10. Re: Automatic Image Compilation
                            JJMack Most Valuable Participant

                            A while back I create some scripts to create collages. The work in CS2, CS3, CS4, CS5 and CS6.  Each version of Photoshop script seems to have a bug or do thing a little differently or did not have a needed function or Photoshop would change its behavior a bit. I think CS3 was the only release I did not have to add some code for. While CS6 I did not have to add code some script will fail if one Photoshop interpolation preference is set to Adobe default "Bicubic Automatic" for Adobe failed to add the code in CS6 scripting to support that setting and an internal error is generated retriving  that setting when it set to "Bicubic Automatic" 

                             

                            Basicly my scripts can place in any size  and image format files that Photoshop supports. Placed images are sized for the bounds of the image area and resized to just cover an area that size. The areas is loaded as a selection and the resized images is aligned to the area and masked with that selection to virtualy crop the positioned image to the shape of the the image area.  To make thing easy on myself my templates your B image files are PSD files with Alpha Channels wihch mapped image locations. I could not get away with a PNG file for I wanted to populate more then a single image.  As Michael wrote there are many way to do things. Some ways better then othere depending on what you needs are ans what you want to support. Knowing thing like aspect ratio of all A images are 2:3 and all opening are also 2:3 may make codeing easer or shorted the code path.

                             

                            Know all requirements before you design your process. Know also how Photoshop works.  I did not know Photoshop only supports up the 53 Alpha Channels.  Because of that I did not find out the my design was limited to collages with fewer then 54 images.  I only found that when I modified someone's Collage Template generating script to create templates compatable with my scripts. When I tried to generate a template with more the 53 images Photoshop let me know I had exceeded the its Alpha Channel limit....

                            • 11. Re: Automatic Image Compilation
                              Michael L Hale Level 5

                              I know that you are new to scripting from one of your posts so maybe you don't understand what information is needed to be of any help. Unless a script is working with already opened image it needs to know where to find the image file before it can open one.

                               

                              It shouldn't be a problem if the templates are not in the same folder. It shouldn't be a problem to work with more than one person image. But without knowing more about where the images are or how you want the user to select them if you want prompts( and I assume you wouldn't in a batch process ) I can't be of much help.

                               

                              You can make a folder object and get all the png files in that folder doing something like this:

                               

                              var templateFolder = new Folder(("/c/Greeting Cards - Resized");

                              var templateFiles = templateFolder.getFiles(/\.png$/i);

                               

                              You can then loop through the template files like this:

                               

                              for(var index = 0; index < templateFiles.length;index++){

                                 var tempDoc = app.open(templateFiles[index]);

                                 // do whatever

                              }