7 Replies Latest reply: Aug 29, 2013 10:51 PM by znarkus RSS

    How do I create an action to crop to a specific aspect ratio?

    DVDmike Community Member

      I have images of varying sizes that I need to all be cropped to a specific aspect ratio in the center of the image and extending either to the side edges or the top and bottom depending on using the maximum portion of the image.  How do you set up an action to do this.

       

      If I were to do this by hand, I would use the rectangle marquee, change to fixed ratio, and drag from one edge to the other.  They move the selection to the center (as best I could) .  Then do Image<Crop?

       

      But I need to automate this process and have have it exactly positioned in the center.  Can this be automated in an action?  If so, how?

       

      FYI, I have CS5 (and CS6).  I am comfortable using the image processor.  So as long as I have an action set up, I can process all of my images using the image processor.

       

      I also have lightroom 4 if there is some way to do it there.

       

      Message was edited by: DVDmike

        • 1. Re: How do I create an action to crop to a specific aspect ratio?
          Trevor.Dennis Community Member

          I've always used Irfanview which is free, and very easy to use.   

           

          http://www.irfanview.com/

          • 2. Re: How do I create an action to crop to a specific aspect ratio?
            DVDmike Community Member

            Does this work as a plug in to Photoshop so that I can batch process via an action?  And does it have the ability to crop center to a specific aspect ratio?  I don't specifically see that feature listed?

            • 3. Re: How do I create an action to crop to a specific aspect ratio?
              Paul Riggott Community Member

              This is possible using a script. An action can be created to run the script and that action used in batch/Image Processor.

               

              The following script needs to be amended with the ratio you require, make sure the largest number is first!

               

               

              main();
              function main(){
              if(!documents.length) return;
              var strtRulerUnits = app.preferences.rulerUnits;
              app.preferences.rulerUnits = Units.PIXELS;
              var W = activeDocument.width;;
              var H = activeDocument.height;
              //Amend ratio to suit
              var ratio = "5:4";
              ratio = ratio.split(":");
              if(W<H) ratio=ratio.reverse();
              var Hpics =(W/Number(ratio[0]))*Number(ratio[1]);
              if(Hpics > H){
                  Wpics = (H/Number(ratio[1]))*Number(ratio[0]);
                  Hpics = H;
                  }else{
                      Wpics = W;
                      }
              var Left,Top,Right,Bottom=0;
              if (W==Wpics) Left = 0; Right = W; 
              if (W!=Wpics) {
                  var diffw = (W-Wpics)/2;
                  Left = diffw; Right = Wpics+diffw;
                  }
              if (H==Hpics) Top = 0; Bottom = H;
              if( H!=Hpics){
                  var diffh = (H-Hpics)/2;
                  Top = diffh; Bottom = Hpics+diffh;
                  }
              activeDocument.selection.select([[Left,Top],[Right,Top],[Right,Bottom],[Left,Bottom]], SelectionType.REPLACE, 0, false); 
              executeAction(charIDToTypeID( "Crop" ), new ActionDescriptor(), DialogModes.NO );
              app.preferences.rulerUnits = strtRulerUnits;
              }
              
              
               

               

              The script needs to be put in the Applications presets/scripts folder and Photoshop re-started so that it picks up the new script.

              To create the action start a new recording and select Insert Menu Item from the action palette fly-out menu the File _ Scripts and select the script. Stop the recording. This action should now crop the document to the correct ratio.

              • 4. Re: How do I create an action to crop to a specific aspect ratio?
                JJMack Community Member

                In my free crafting Action package I have included  12 Scripts I wrote to be use in actions.  Two of these scripts are Photoshop Plug-in recording these in actions will record the setting used in its dialog during the action recording into the action step.  When the action is played the action will pass the settings to the script and the script will bypass displaying its dialog.    These two Script Plug-ins are ideal for doing what you want to do.  The first script is AbsoluteAspectRatio.jsx using its dialog you can have it create a centerd selection that has the Aspect Ratio you want the script will set the selection to maintain the images orintation LandScape or Portrait.So Landscap and Portarit images can be batched process in a single batch run. The second Plugin script is LongSidePrintLength.jsx This script just sets the documen'ts print DPI so it prints the size you set in it dialog or action.

                 

                With these Plug-in it simple to create center crops action that can be batched for any aspect ration and print size"

                Step 1 menu File>Automate>AspectRatioSelection  set dialog for desired aspect ratio, center option, rectangle option, selection type replace, no feather

                example 4 5 aspect ratio

                Step 2 menu Image>Crop

                Step 3 LongSidePrintLength... Set 10

                 

                The above action will crop image to a 4:5 aspect ration and set the document to print 8"x10"  Portrait or 10"x8" Landscape.

                 

                Crafting Actions Package Contains

                • Action Actions Palette Tips.txt
                • Action Creation Guidelines.txt
                • Action Dealing with Image Size.txt
                • Action Enhanced via Scripted Photoshop Functions.txt
                • CraftedActions.atn Sample Action set includes an example Watermarking action
                • Sample Actions.txt Photoshop CraftedActions set saved as a text file.
                • 12 Scripts for actions

                Download

                • 5. Re: How do I create an action to crop to a specific aspect ratio?
                  znarkus

                  Thanks for these scripts!

                  I noticed an (for me) issue with AspectRatioSelection though: even though I set aspect ratio to 6:4 it decides to crop some images 4:6.

                  It works with images that are portrait, but those that are more square are cropped in 4:6.

                  • 6. Re: How do I create an action to crop to a specific aspect ratio?
                    JJMack Community Member

                    Yes I wrote AspectRatioSelection to take the document orientation into consideration an use the ratio you set orientated to the documents orientation.  So an action to crop Landscape and Portraits for a 4X6 paper would be easy to record. You will notice the dialog has two numbers fields not a Width and Height fields no orientation is implied. Its companion LongSidePrintLength.jsx the let you set the correct print dpi after the aspect ratio crop.

                     

                    I did create a plug-in where you could specify an absolute aspect ratio that also had an orient option to work like AspectRatioSelection.  So with that plug-in you would able to crop a landscape to a portrait and portrait to landscape. Hover the image composition changes so much when you do often you can't get an acceptable composition.  The dialog also became hard  to describe well for me.  I program better then I do English.  The orient check box made width and height meaningless and the aspect ratio not absolute and wasn't easly explained. This is what it look like.

                     

                    aar.jpg

                     

                    znarkus wrote:

                     

                    Thanks for these scripts!

                    I noticed an (for me) issue with AspectRatioSelection though: even though I set aspect ratio to 6:4 it decides to crop some images 4:6.

                    It works with images that are portrait, but those that are more square are cropped in 4:6.

                    I don't follow your more square thing.  If you have two documents one 4x6 and the other 6x4 which is more square?

                     

                    The way it works biased on you documents orientation. If your document is wider then tall it landscape if it taller the wide it a Portrait. If It square the plugin defaults it to Landscape.

                     

                    So it makes no difference if you set 4 and 6 or 6 and 4 or 3 and 2 or 2 and 3 

                     

                    Landscape and square document will see landscape selection with a or 3:2 aspect ratio selection or path the is either rectangle or oval

                    Portraits documents will see a Portrait selection with a 2:3 aspect ratio selection or path the is either rectangle or oval

                     

                    Message was edited by: JJMack

                    • 7. Re: How do I create an action to crop to a specific aspect ratio?
                      znarkus Community Member

                      Thanks for your response!

                      About the square images: I realised after I wrote that, that the images were "landscape", but looked square (they were something like 501x500px).

                      I modified the script to suit my needs. Thank you for the script!