Current position:  Home > Default > Batch Resize photos Larger than a certain size.

Batch Resize photos Larger than a certain size.

Time:December 10
Advertisement
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.
Advertisement
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 );