4 Replies Latest reply on Jun 20, 2013 8:27 PM by Van Middleton

    Script to make landscape and portrait images square


      I am new to scripting and could use a little help.


      We are currently rebuilding our enitre website from the ground up. New look, new products, new images (about 2,800 of them). Here is where it gets tricky.


      Some of the photos are landscape and others are portrait. I have resized the images to the required 200 pixels wide, but we are now running into the issue where they also need to be 200 pixels high. I could just separate them into two folders, landscape and portrait, and run two different actions, but I would prefer to run an if/than script if possible. Can someone tell me if this is possible in Photoshop? These are images of products, so there is no background.


      If the image is larger than 200 pixels high, reduce the image to 200 pixels high. Then, enlarge the canvas to 200 pixels.


      If the image is smaller than 200 pixels high, Then enlarge the canvas to 200 pixels high.


      These are all 72 dpi, RGB images. Any help would be greatly appreciated.

        • 1. Re: Script to make landscape and portrait images square
          churley35 Level 1

          I have found this JaveScript that does what I need on the PC side, but keep getting an error when it tries to save the image. Is anyone able to recreate this into AppleScript with a correct save function... I am running a MAC, so I need an AppleScript. My coworker has Photoshop on the PC and is trying to use this JavaScript below.



          //  resize canvas  & export JPG



          #target photoshop



          // Save current dialog preferences

          var startDisplayDialogs = app.displayDialogs;    


          // Save current unit preferences

          app.displayDialogs = DialogModes.NO


          // Source and Destination Folders

          var inputFolder = Folder.selectDialog("Select the Source folder");

          var outputFolder = Folder.selectDialog("Select the Destination folder");




          function ProcessImages() {

            var filesOpened = 0;


             if ( inputFolder == null || outputFolder == null) {

             if ( inputFolder == null) {

                      alert("No Source folder selected");


              if ( outputFolder == null) {

                 alert("No Destination folder selected");




          var fileList = inputFolder.getFiles();

            for ( var i = 0; i < fileList.length; i++ ) {

                 if ( fileList[i] instanceof File && ! fileList[i].hidden) {

                           open( fileList[i] );






            return filesOpened;



          function ExportJpeg(filePrefix, fileSuffix){



          var docRef = app.activeDocument;

          var docName = app.activeDocument.name.slice(0,-4);

          var jpegOptions = new JPEGSaveOptions();

          jpegOptions.quality = 8


          docRef.bitsPerChannel = BitsPerChannelType.EIGHT

          jpegFile = new File( outputFolder + "//"  + filePrefix + docName + fileSuffix );

          //Save Document As

          docRef.saveAs(jpegFile, JPEGSaveOptions, true, Extension.LOWERCASE);




          catch (e)


          alert("Error when attempting to save the image. \r\r" + e);





          function ResizeCanvas()


             function CanvasSize() {


                var docRef = app.activeDocument;

                var docWidth = docRef.width.as("px");

                var docHeight = docRef.height.as("px");  


                if (docWidth < docHeight)


                      docRef.resizeCanvas(docWidth = docHeight, docWidth, AnchorPosition.MIDDLECENTER);


                else if (docWidth > docHeight)


                      docRef.resizeCanvas(docHeight = docWidth, docHeight, AnchorPosition.MIDDLECENTER);


                else if (docWidth == docHeight)






          var docRef = app.activeDocument;


          var savedState = docRef.activeHistoryState;



          app.displayDialogs = DialogModes.NO;

          ExportJpeg("", ".jpg");


          docRef.activeHistoryState = savedState;


          docRef = null;



          // Reset preferences

          app.displayDialogs = startDisplayDialogs;


          alert("Operation Complete!" + "\n" + "Images were successfully exported to:" + "\n" + "\n" + outputFolder.toString().match(/([^\.]+)/)[1] + "/");

          • 2. Re: Script to make landscape and portrait images square
            JJMack Most Valuable Participant

            You need to make your images square 1:1 aspect ratio during the process so they are not distorted during the resize to 200x200pixels. You can make your images square by adding blank canvas or cropping off some image areas. Javascript should work on a Mac.


            The above script should work on a Mac.   I just took a quick look at it does not resize the image to 200x200 it add canvas to portrait width to make them square and add canvas the Lanscape height to make them square. Leaves  square image square. There is no resize to 200x200 that i can see. If you do not want to crop you images to should be able to run the above script then create an single step action Image Size ckect resample and constrain and set the width or hight in the top section to 200.  Then use menu File>Automate>Batch point the where the above script exported the jpeg files.. Point to you resize action and have the Batch proces save the files in place overwriting the images made square by adding canvas. Or you could add a image resize to 200x200 in the script just before the export after the canvas resize.


            In my Crafring Action Package there are Script I have written to be used in actions. One is a Plug-in named AspectRatioSelection a simple two step action is all you need to record to be used with Photoshop Script "Image Processor". Record the action  Step one  use menu File>Automate>AspectRatioSelection. In the AspectRatioSelection dialog set a in the numbers 1 and 1 for a 1:1 Aspect ratio square slection, check Center for the selection to be centered in the image.  Select the  radio option buttons Rectangle and Replace and enter 0 no feathering. Click OK.  Step two Menu Image>Crop. Stop recording and save the action set.  The run the Image Processor script menu File>Scripts>Images processor. or from the bridge menu Tools>Photoshop>Image Processor.  In the Image Processor dialog set the input section to process the files you want to crop and size. Set the ouput section to where you want the files saved. Set the file type section to save a Jpeg file and set the reszize to fit 200x200pixels. Set the botton section to run your action theat crops image to its center square.

            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


            • 3. Re: Script to make landscape and portrait images square

              Bug Alert



              docRef.saveAs(jpegFile, JPEGSaveOptions, true, Extension.LOWERCASE);


              docRef.saveAs(jpegFile, jpegOptions, true, Extension.LOWERCASE);


              or you will really be wondering why your files are being saved at quality level 3...

              • 4. Re: Script to make landscape and portrait images square
                Van Middleton

                Us the "Fit Image" option in photoshop - File - automate - Fit Image.