22 Replies Latest reply on Apr 25, 2017 9:09 AM by schroef

    Conditional Image Resizing

    Jimmery Level 1

      Hi

       

      Ive been using batch processing in photoshop since version 7, however Ive recently found the need to do conditional actions - which apparently can be done with photoshop scripting.

       

      What Im trying to do is resize an image if it is over a certain size (document dimensions that is) but leave it at its original size if not.

       

      If I was coding in C or something Id probably do (forgive the poor pseudo code)

       

      if(document.height*document.width>640000){document.resize("50%");}

       

      Can anybody point me in the right direction for creating this type of script in photoshop CS4?

       

      Many thanks

       

      James

        • 1. Re: Conditional Image Resizing
          Muppet Mark-QAl63s Level 4

          Is that right that you want to 1/4 the area?

           

          if (app.activeDocument.width.as('px')*app.activeDocument.height.as('px') > 640000) {
               app.activeDocument.resizeImage(undefined,undefined, app.activeDocument.resolution/2, ResampleMethod.BICUBICSHARPER);
          }
          

          1 person found this helpful
          • 2. Re: Conditional Image Resizing
            Jimmery Level 1

            Yes, I want to halve the height and halve the width, which would result in a quarter of the area.

             

            Many thanks for your helpful answer!

            • 3. Re: Conditional Image Resizing
              JJMack Most Valuable Participant

              If you have CS5 you can actually do what what you want in an action. Adobe added an option to their FitImage plugin script to not resize images the are  that all ready fit  within an image size. So they are not upsized. All Larger images will be be resized to fith with in an image size.  They did also incude a bug in that code if it hasn't been fixed by now some day they may.  Its easy to fix on your own.  Since I modified FitImage to resample using differen methods of bicubic depending on direction I fixed the bug in my bersion.

              • 4. Re: Conditional Image Resizing
                Muppet Mark-QAl63s Level 4

                I have not looked at that… but the above is based on image 'area' and not longest edge.

                • 5. Re: Conditional Image Resizing
                  Michael L Hale Level 5

                  I am sorry to say that the bug is mine. It has been fixed and the new script can be found at http://ps-scripts.com/bb/viewtopic.php?f=10&t=3370. Adobe has the corrected version. I had hoped that it would have been included in one of the updates.

                  • 6. Re: Conditional Image Resizing
                    JJMack Most Valuable Participant

                    If you have CS5 you can actually do what what you want in an action. Adobe added an option to their FitImage plugin script to not resize images the are  that all ready fit  within an image size. So they are not upsized. All Larger images will be be resized to fith with in an image size.  They did also incude a bug in that code if it hasn't been fixed by now some day they may.  Its easy to fix on your own.  Since I modified FitImage to resample using differen methods of bicubic depending on direction I fixed the bug in my version.

                     

                    This of course is not  making the image 1/4 size like your writing about it more for fitting images to something like an HDTV 1920x1080 size.  Large images will be scaled to fit the screen small images will not be scaled up in size you don't want to big blured images out of thumbnails and. Web size images will look good.

                     

                    My appen was not complete the adobe site messed it up.

                    • 7. Re: Conditional Image Resizing
                      JJMack Most Valuable Participant

                      Michael

                       

                      Everyone makes mistakes I have learned so much from you and your code.  I make more mistakes then the average bear.  My mind is not wired as most I'm dyslexic can't  type or spell in other words I'm very accident prone.  I spend more time in error recovery then actual code.  I dislike javascript for it is case sensitive my scripting language of choice would be "Rexx".  I don't think I'll ever be able to decipher the syntax of a regular expression my eyes just can't see them me think.

                      • 8. Re: Conditional Image Resizing
                        Michael L Hale Level 5

                        If you are on Windows check out an app called RegexBuddy. It helps creating regular expression. More inportantly, you can enter an existing and it will explain it to you char by char.

                        • 9. Re: Conditional Image Resizing
                          JJMack Most Valuable Participant

                          Michael

                          Thank you for the pointer to RegexBuddy I may look into it.  The major problem I have is a visual thing it not that I'm blind or visuallu impaired other then being a little colorblind.  It that my eyes and mind does not to text well charcters get transpsoed in the process and do not alwys seem to be able to decoded text in the correct order. Regex syntax for me is mind boglling for me even if I took the time to learn and fully understand Regex looking at them for me would still be a choir. This old dog does not have the desire to learn new trick and is forgetting many of the old trick I could do without even thinking.  I'm just a lazzy old dog these days enjoying the grandchildern...

                          • 10. Re: Conditional Image Resizing
                            Mac-Burg-33 Level 1

                            Hi I am trying to create a similar script, which will close images that are opened and are smaller than 1800, but pass on anything over 1800. I have most of the script sorted but it is coming up with an error. Here is the script as I have saved and tested it -

                             

                            app.displayDialogs = DialogModes.NO;
                            var strtRulerUnits = preferences.rulerUnits;
                            preferences.rulerUnits = Units.PIXELS;
                            var testset2 = '1800 Batch Final';
                            var taction2 = '1800+ Resize';

                             

                            if (activeDocument.height < 1800 || activeDocument.width < 1800)
                              {
                              activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                              }
                            else
                              {
                              app.doAction(1800 Batch Final,1800+ Resize);
                              activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                              }
                            preferences.rulerUnits = strtRulerUnits;

                             

                             

                            I am using Photoshops "Batch" processor to run this script as part of an action, on a folder of randomly sized images, so that if the image opened is bigger than 1800 it gets passed onto another action to resize the image, save and close. My action set is called "1800 Batch Final" and the action within this set that is supposed to be played on files larger than 1800 is called "1800+ Resize". The error message that comes up is -

                             

                            Error Line 13 - app.doAction(1800 Batch Final,1800+ Resize);

                             

                            I am new to scripting so any help to get this working would be great, thanks.

                            • 11. Re: Conditional Image Resizing
                              Jimmery Level 1

                              Its been a long time since I did any scripting in Photoshop, however maybe you need to change line 13 to something like:

                               

                              app.doAction('2500 Batch Final','2500+ Resize');

                               

                              ? Not sure if this helps - sorry!

                              • 12. Re: Conditional Image Resizing
                                Mac-Burg-33 Level 1

                                Thanks Jimmery, that was the main part of the mistake and it gave me a better understanding of how to get the script running properly. I ended up with -

                                 

                                app.displayDialogs = DialogModes.NO;
                                var strtRulerUnits = preferences.rulerUnits;
                                preferences.rulerUnits = Units.PIXELS;
                                var testset2 = '1800 Batch Final';
                                var taction2 = '1800+ Resize';

                                 

                                if (activeDocument.height < 1800 || activeDocument.width < 1800)
                                  {
                                  activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                                  }
                                else
                                  {
                                  app.doAction('1800+ Resize','1800 Batch Final');
                                  activeDocument.close;
                                  }
                                preferences.rulerUnits = strtRulerUnits;

                                 

                                So the action/actionset in the "else" command were around the wrong way also. One more problem I need to overcome, the script would work better if it would only resize images if the shortest dimension was larger than 1800. In its current state it closes images if any dimension is smaller than 1800, yet some images can be less than 1800 in the shortest side yet larger on the longest side. These need to stay open for further processing by the action, so what is the command line for less than on the shortest side? I'll do more research but any help here would be great.

                                • 13. Re: Conditional Image Resizing
                                  Jimmery Level 1

                                  Maybe try...

                                   

                                  var shortestSide;

                                   

                                  if (activeDocument.height < activeDocument.width)

                                  {

                                       shortestSide=activeDocument.height;

                                  }else{

                                       shortestSide=activeDocument.width;

                                  }

                                   

                                  if (shortestSide < 1800)

                                    {

                                    activeDocument.close(SaveOptions.DONOTSAVECHANGES);

                                    }

                                  else

                                    {

                                    app.doAction('1800+ Resize','1800 Batch Final');

                                    activeDocument.close;

                                    }

                                  preferences.rulerUnits = strtRulerUnits;

                                  • 14. Re: Conditional Image Resizing
                                    Mac-Burg-33 Level 1

                                    Thanks Jimmery, by looking at the script it seems that this should work, but it doesn't and still seems to close the image.

                                    if the shortsest dimension in the image is over 1800 it works, but an image might be 1500x2600, and this 1500 seems to cause the image to close rather than stay open.

                                     

                                    I have also tried changing the "shortest" to "longest" and this seems to make no difference. It would make more sense I guess for the script to determine the longest side of the image, and if this is less than 1800, close, if the longest side is larger than 1800, stay open and go onto play the actions.

                                     

                                    Sorry for my lack of scripting knowledge here, I can understand the script to a ceratin degree by reading through it, but don't know the language at all which makes it hard to modify the script. If you have a solution to the longest side please do post, cheers Shaun.

                                    • 15. Re: Conditional Image Resizing
                                      Jimmery Level 1

                                      ok - try this instead

                                       

                                      var longestSide;

                                       

                                      if (activeDocument.height > activeDocument.width) {

                                           longestSide=activeDocument.height;

                                      }else{

                                           longestSide=activeDocument.width;

                                      }

                                       

                                      if (longestSide < 1800)

                                        {

                                        activeDocument.close(SaveOptions.DONOTSAVECHANGES);

                                        }

                                      else

                                        {

                                        app.doAction('1800+ Resize','1800 Batch Final');

                                        activeDocument.close;

                                        }

                                      preferences.rulerUnits = strtRulerUnits;

                                      • 16. Re: Conditional Image Resizing
                                        Mac-Burg-33 Level 1

                                        Nice one that seems to have it cracked, tested on a range of images and it runs perfectly.

                                         

                                        Thank you for the persistence and solution, much appreciated. and a great tool to have in my workflow.

                                         

                                        Cheers Shaun.

                                        • 17. Re: Conditional Image Resizing
                                          JamesAdler Level 1

                                          Bumping an old thread on the same them in the hope of some answers. I discovered this thread searching for a solution.

                                           

                                          I run photographic/Photoshop sessions for senior citizens and as simple as it may sound, they struggle to resize to our required sizes for competition entries.

                                           

                                          I would like to make it as easy as possible for them to do if possible, changing the FitImage script or something similar. 

                                           

                                          If the longest side of the image is the width then I need to resize so that the width is say 1280px (constraining proportions)

                                           

                                          If the longest side of the image is the height then I need to resize so that the height is say 768px (constraining proportions)

                                           

                                          If the sides are equal then I need to resize so that both sides are 768px.

                                           

                                          If any of the sides are actually smaller than the above sizes, is it possible show a warning with the option to either proceed or cancel.

                                           

                                          Cheers

                                           

                                          James

                                          • 18. Re: Conditional Image Resizing
                                            JJMack Most Valuable Participant

                                            Fit image is a Plug-in script that supports actions so all you need do is to record an action for each size you need. When the actions are played the recorded settings are passed to  the Fit Image plugin and the plug-in bypasses displaying its dialog and uses the passed sizes. Images are not distorted the original aspect ratio is maintained,  The image is resampled to fit within the size set as large as it can be. Alway start with the largest best quality image you have, resize and save a new image do not save over your best image. You do not want to resize a resized image. For every time you resize an image up or down in pixel size you loose some image quality. So keep and protect you original good quality images.

                                             

                                            Action FitToHDTV

                                            record menu File>Automate>Fit Image in dialog set width 1920 height 1080

                                            hdtv.jpg

                                             

                                            Action FitToUXGA

                                            record menu File>Automate>Fit Image in dialog set width 1600 height 1200

                                             

                                            Action FitToXGA

                                            record menu File>Automate>Fit Image in dialog set width 1024 height  769

                                             

                                            Action FitToWebPage3:2

                                            record menu File>Automate>Fit Image in dialog set width 600 height 200

                                             

                                            Action FitToWebPage4:3

                                            record menu File>Automate>Fit Image in dialog set width 800 height 600

                                             

                                            Message was edited by: JJMack

                                            • 19. Re: Conditional Image Resizing
                                              JamesAdler Level 1

                                              Hi, thanks for replying.

                                               

                                              Your response has caused me to rethink and discover that it is not always going to be possible to achieve what I want. It relies entirely on the aspect ratio of the image that you start out with. A complete oversight.

                                               

                                              Cheers

                                               

                                              James

                                              • 20. Re: Conditional Image Resizing
                                                JJMack Most Valuable Participant

                                                JamesAdler wrote:

                                                 

                                                Hi, thanks for replying.

                                                 

                                                Your response has caused me to rethink and discover that it is not always going to be possible to achieve what I want. It relies entirely on the aspect ratio of the image that you start out with. A complete oversight.

                                                James  If you know the size you want it would be easy to do if a fitted center crop would fill your needs.  However I must warn you that center crops do not work all that when you change Landscape to portraits and portraits to landscapes. Here are some examples of what happen when there are orientation mismatches.

                                                http://www.mouseprints.net/old/dpr/BatchCollage/Collage_1_4_6x4.jpghttp://www.mouseprints.net/old/dpr/BatchCollage/Collage_1_4_4x6.jpghttp://www.mouseprints.net/old/dpr/BatchCollage/Collage_1_4_6x4+FDNYonTopG.jpghttp://www.mouseprints.net/old/dpr/BatchCollage/Collage_1_4_4x6+FDNYonTopG.jpg

                                                Documentation and Examples

                                                • 21. Re: Conditional Image Resizing
                                                  pierBover Level 1

                                                  Hi all

                                                   

                                                  I have created 2 scripts for conditional resizing. Hopefully this will come handy for others with just minimal modifications, or as a starting point for their own script.

                                                   

                                                  This one is a simple conditional resize. You just have to change the desired area dimensions, and change upscale to 'true' if you don't mind images to be upscaled.
                                                  Conditional resize image script for Photoshop. No save, only resize to desired area. · GitHub

                                                   

                                                  This one is a bit more complex. It does conditional resize for 2 size versions and then saves each version to a folder.
                                                  Conditional resize image script for Photoshop. Creates thumbnail and large version and saves in JPEG. · GitHub

                                                   

                                                  If you know nothing about programming and need some little change (or something doesn't work) please tell me.

                                                  • 22. Re: Conditional Image Resizing
                                                    schroef Level 1

                                                    thanks, super useful. was looking for something which could check if its horizontal or portrait. Finally got it!