11 Replies Latest reply on Aug 9, 2010 6:15 AM by Xeon64

    Batch Resize photos Larger than a certain size.

    Xeon64 Level 1

      Is there a way to have a script that searches a directory and any image files that are larger than 1000 x 1000 pixels are resized to 500 x 500 pixels and then saved over righting the sources file?

       

      I am asking because our server has over 40,000 photos all over the place and I would like to reduce the space they take on the server.

        • 1. Re: Batch Resize photos Larger than a certain size.
          lowhow

          Try this:

           

          1. var f = yourFolder.getFiles() to read in all the files in your directory,

          2. then read each file's length f[i].lenght/1000 to get an approximate file size in pixels; for files of size 1000x1000, the value you look for is >=1000

          3. files of that size, do a activeDocument.resizeImage(500, 500, undefined, undefined), but before make sure that the Preferences > Units > Rulers is set to pixels

          • 2. Re: Batch Resize photos Larger than a certain size.
            Michael L Hale Level 5

            I don't think that getting the file's length is the best way to determine if the file needs resizing. Especially if the file is in a format that supports compression. I think you would end up either setting the test value too low and open a lot of files that don't need resizing or set too high and miss files that do.

             

            Also using getFiles() without a file mask will get all files and folders in the source folder. You will need to make sure it is an image file before trying to open in Photoshop to resize.

             

            I think the function below would be a better way to determine if the file needs resizing. It tries to get the XMPMetadata to determine width and height. If that fails because the file does not have metadata it gets the size from Bridge.

             

            var file = new File('~/desktop/_SLP7637.jpg');
            
            var resize = needsResizing( file );
            var resizeStr = ''
            resize? resizeStr = 'needs resizing': resizeStr = 'does not need resizing';
            alert(file.fsName+" "+resizeStr);
            
            function needsResizing( file ){
                try{
                    loadXMPLibrary();
                    var xmpf = new XMPFile( file.fsName, XMPConst.UNKNOWN, 
                                               XMPConst.OPEN_FOR_READ );
                    var xmp = xmpf.getXMP();
                    xmpf.closeFile();
                    var dim = xmp.getProperty( XMPConst.NS_EXIF, 'PixelXDimension', XMPConst.STRING)+','+xmp.getProperty( XMPConst.NS_EXIF, 'PixelYDimension', XMPConst.STRING);
                    dim = dim.split(',');
                    unloadXMPLibrary();
                    if(dim=="undefined,undefined"){
                        var dim = undefined;
                        var res = undefined;
                        var bt = new BridgeTalk;
                        bt.target = "bridge";
                        var myScript = ("var ftn = " + psRemote.toSource() + "; ftn("+file.toSource()+");");
                        bt.body = myScript; 
                        bt.onResult = function( inBT ) {myReturnValue(inBT.body); }
                        bt.send(10);
                 
                        function myReturnValue(str){
                            res = str;
                            dim = str.split(',');
                        }
            
                        function psRemote(file){
                            var t= new Thumbnail(file);
                            return t.core.quickMetadata.width+','+t.core.quickMetadata.height;
                        }
                    }
                }catch(e){unloadXMPLibrary();}
                var res = true;
                if(dim[0]<1001&&dim[1]<1001) res = false;
                return res;
            }
            function loadXMPLibrary(){
                 if ( !ExternalObject.AdobeXMPScript ){
                      try{
                           ExternalObject.AdobeXMPScript = new ExternalObject
                                                                        ('lib:AdobeXMPScript');
                      }catch (e){
                           alert( ErrStrs.XMPLIB );
                           return false;
                      }
                 }
                 return true;
            };
            function unloadXMPLibrary(){
               if( ExternalObject.AdobeXMPScript ) {
                  try{
                     ExternalObject.AdobeXMPScript.unload();
                     ExternalObject.AdobeXMPScript = undefined;
                  }catch (e){
                     alert( ErrStrs.XMPLIB );
                  }
               }
            };
            
            
            • 3. Re: Batch Resize photos Larger than a certain size.
              Muppet Mark-QAl63s Level 4

              I just roughed this out but this would work very very fast locally for me on the mac… Not sure about the server… Oh I can only do this in Bridge but you may be able to in Photoshop…

               

              #target bridge
              
              var f = Folder.selectDialog('Select a Folder to search in?');
              
              var shellString = "mdfind -onlyin ";
              shellString += "'" + f.fsName + "/'";
              shellString += " 'kMDItemPixelHeight >= 1000 || kMDItemPixelWidth  >= 1000' > ";
              shellString += "~/Documents/StdOut.txt";
              
              //$.writeln(shellString);
              
              var sh = app.system(shellString);
              

               

              The output text should be your file list?

              • 4. Re: Batch Resize photos Larger than a certain size.
                Paul Riggott Level 6

                I will have to assume a few things as Files type/s or Photoshop version has not been given.

                If you want to resize jpgs and not bothered about metadata it could all be done in Bridge.

                As an example you could run this code from ExtendScript Toolkit, select your folder in bridge and run the following code.

                It will check the size, resize and overwrite all in Bridge.

                NB: If nothing is selected it will select ALL jpgs otherwise will only select jpgs from the selected files.

                 

                #target bridge
                var thumbs = app.document.getSelection("jpg");
                for(var a in thumbs){
                var md = thumbs[a].synchronousMetadata; 
                var t= new Thumbnail(thumbs[a]);
                if(Math.max(t.core.quickMetadata.width,t.core.quickMetadata.height)>=1000){
                md.namespace = "http://ns.adobe.com/tiff/1.0/";
                var orientation = md.Orientation.replace(/(\w+)(\s+)(.)(\d+)(.)/,"$3$4");
                if(orientation == 'Normal') orientation =0;
                var bm = new BitmapData(thumbs[a].spec);
                bm = bm.rotate(orientation);
                bm = bm.resize(500,BitmapData.bicubicSharper);
                bm.exportTo(new File(thumbs[a].spec),80); //80 is the quality
                     }
                }
                

                • 5. Re: Batch Resize photos Larger than a certain size.
                  Xeon64 Level 1

                  Thanks for all of the input. I should be able to get something to work from all the responses. I am using photoshop CS5 64bit on a Windows 7 workstation. The file type I am most interested in is jpgs. Our sales team has been using digital cameras and saving the files on the server for several years. I would like to reduce the space that the files are using to reduce backup costs.

                  • 6. Re: Batch Resize photos Larger than a certain size.
                    Xeon64 Level 1

                    Okay, I am trying to get the bridge targeted scripts to work and not having success. I open the ExtendScript Tool Kit CS5 past the script and click run. The bridge opens and nothing happens. I even selected a folder in bridge then click run again in ExtendScript and nothing happens.

                     

                    My scripting knowledge is limit. I use to have in house IT make things work now I do not and have to fiddle my way through scripting now.

                    • 7. Re: Batch Resize photos Larger than a certain size.
                      Paul Riggott Level 6

                      The script does not give any messages at all it just does what it needs to do. Have you checked the file sizes?

                       

                      I have added a couple of lines to the code so that there will be  a prompt telling you how many JPGs it will be checking, (you will need to Okay this so that the script can continue) at the end it will tell you how many JPGs it has modified.

                      #target bridge
                      var thumbs = app.document.getSelection("jpg");
                      alert("Checking "+ thumbs.length + " files");
                      var jpgs = 0;
                      for(var a in thumbs){
                      var md = thumbs[a].synchronousMetadata; 
                      var t= new Thumbnail(thumbs[a]);
                      if(Math.max(t.core.quickMetadata.width,t.core.quickMetadata.height)>=1000){
                      jpgs++;
                      md.namespace = "http://ns.adobe.com/tiff/1.0/";
                      var orientation = md.Orientation.replace(/(\w+)(\s+)(.)(\d+)(.)/,"$3$4");
                      if(orientation == 'Normal') orientation =0;
                      var bm = new BitmapData(thumbs[a].spec);
                      bm = bm.rotate(orientation);
                      bm = bm.resize(500,BitmapData.bicubicSharper);
                      bm.exportTo(new File(thumbs[a].spec),80); //80 is the quality
                           }
                      }
                      alert(jpgs + " JPEGs have been modified");
                      
                      • 8. Re: Batch Resize photos Larger than a certain size.
                        Xeon64 Level 1

                        I got the script to work on a single folder with no other folders within it. I select all the files an run and it works. However if I select a folder that has several sub folders with Jpgs it does nothing. It only resizes what is selected.

                        • 9. Re: Batch Resize photos Larger than a certain size.
                          Paul Riggott Level 6

                          The best bet would be to do a select all (ctrl/a) then, and run the script for each folder.

                          • 10. Re: Batch Resize photos Larger than a certain size.
                            Paul Riggott Level 6

                            If you wanted to do sub folders all in one go you can select "View - Show Items From Subfolders" then select all and run the script, this might save you some time.

                            • 11. Re: Batch Resize photos Larger than a certain size.
                              Xeon64 Level 1

                              Thanks. It works perfect when show all is selected.