7 Replies Latest reply on Aug 20, 2014 1:43 PM by timlogochair

    Script or Action - Make image square

    Camst4 Level 1

      Need to add this to an action, rather than making all images square manually before batching them all.

       

      - All 500+ images are various sizes both portait, landscape and possibly square already.

      - They are all cropped tightly around the product with a white background.

       

      So currently: I just use canvas size, anchor to the centre and change the smallest pixel dimension to make it square, so none of the product is cropped.

       

      Any ideas?

       

      I couldn't get JJMack's aspect ratio script to help me.

      Automate -> Fit Image will take it to 1000 pixels but still won't be square.

       

       

      EDIT: Oh my. I just found this, looks like it'll do the job. Will test some more in the morning.

       

      var savedRuler= app.preferences.rulerUnits;

      app.preferences.rulerUnits = Units.PIXELS;

      var w = app.activeDocument.width;

      var h = app.activeDocument.height;

      if(w>h) app.activeDocument.resizeCanvas (w, w, AnchorPosition.MIDDLECENTER);

      if(w<h) app.activeDocument.resizeCanvas (h, h, AnchorPosition.MIDDLECENTER);

      //if w==h already square

      app.preferences.rulerUnits = savedRuler;

        • 1. Re: Script or Action - Make image square
          Michael L Hale Level 5

          ! would add is a check of the background color and set it to white if needed.

          • 2. Re: Script or Action - Make image square
            Camst4 Level 1

            Ahh yep ok. How do I do that?

            • 3. Re: Script or Action - Make image square
              Michael L Hale Level 5

              Near the top where you set the ruler add these lines.

               

              var startColor = app.backgroundColor;
              if(startColor.rgb.hexValue != 'FFFFFF'){
                  var white = new SolidColor();
                  white.rgb.hexValue = 'FFFFFF';
                  app.backgroundColor = white;
              }
              

               

              and at the bottom add this line

               

              if(white != undefined) app.backgroundColor = startColor;
              
              • 4. Re: Script or Action - Make image square
                Camst4 Level 1

                Brilliant, thank you Michael.

                • 5. Re: Script or Action - Make image square
                  timlogochair Level 1

                  I have a need for this script as well...is this the entire script?  If not, can you post the complete script?

                   

                  I just need to increase either the canvas width (as white area for the added width/height) or the canvas height to match the longest side of an image so they they are perfectly square.  No cropping or reduction in image sizes.  These are product images on white background.

                   

                  Is this the full script?

                   

                  var startColor = app.backgroundColor; 
                  if(startColor.rgb.hexValue != 'FFFFFF'){ 
                      var white = new SolidColor(); 
                      white.rgb.hexValue = 'FFFFFF'; 
                      app.backgroundColor = white; 

                   

                  var savedRuler= app.preferences.rulerUnits;

                  app.preferences.rulerUnits = Units.PIXELS;

                  var w = app.activeDocument.width;

                  var h = app.activeDocument.height;

                  if(w>h) app.activeDocument.resizeCanvas (w, w, AnchorPosition.MIDDLECENTER);

                  if(w<h) app.activeDocument.resizeCanvas (h, h, AnchorPosition.MIDDLECENTER);

                  //if w==h already square

                  app.preferences.rulerUnits = savedRuler;

                  if(white != undefined) app.backgroundColor = startColor; 

                  • 6. Re: Script or Action - Make image square
                    timlogochair Level 1

                    So I went ahead and tried the script I posted previously, and it works great for my needs.  It even works great saved as a step in an action.  However, when I try to batch run this action on a group of images in a folder (from bridge), the process is interrupted for every single image with the following dialogue box.  I'm actually not sure what to put in here manually anyway, but the main question is "why" is this popping up when I run as batch, but not when I open an image and then run the action?  How can I suppress the dialogue and have it work in batch mode just like when I manually run the action?

                     

                    • 7. Re: Script or Action - Make image square
                      timlogochair Level 1

                      Hopefully this will help someone down the road...here is my final script that works in batch mode from bridge:

                       

                      var startColor = app.backgroundColor;

                          if(startColor.rgb.hexValue != 'FFFFFF'){

                              var white = new SolidColor();

                              white.rgb.hexValue = 'FFFFFF';

                              app.backgroundColor = white;

                          }

                       

                      var savedRuler= app.preferences.rulerUnits;

                      app.displayDialogs = DialogModes.NO;

                      app.preferences.rulerUnits = Units.PIXELS;

                       

                      var w = app.activeDocument.width;

                       

                      var h = app.activeDocument.height;

                       

                      if(w>h) app.activeDocument.resizeCanvas (w, w, AnchorPosition.MIDDLECENTER);

                       

                      if(w<h) app.activeDocument.resizeCanvas (h, h, AnchorPosition.MIDDLECENTER);

                       

                      //if w==h already square

                       

                      app.preferences.rulerUnits = savedRuler;

                       

                      if(white != undefined) app.backgroundColor = startColor;