5 Replies Latest reply on Apr 16, 2015 7:51 PM by jeremyrklein

    How do I crop a percentage from the edges of an image?

    jeremyrklein

      I'm using a batch crop and straighten script to separate scanned photos, but there is always a little bit where the crop hasn't been performed perfectly. I'd like to find a way to crop 1-2% off all edges of my document/image. I'm not worried about loosing image integrity. Would be nice to have a script to add to my existing script. Essentially this step will make my edges crisp and eliminate any slivers of white space or bent corners.

       

      Here is the script that I'm using to divide my scanned photos:

      #target Photoshop 

      app.bringToFront; 

      var inFolder = Folder.selectDialog("Please select folder to process");  

      if(inFolder != null){ 

      var fileList = inFolder.getFiles(/\.(jpg|tif|psd|)$/i); 

      var outfolder = new Folder(decodeURI(inFolder) + "/Edited"); 

      if (outfolder.exists == false) outfolder.create(); 

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

        if(fileList[a] instanceof File){ 

          var doc= open(fileList[a]); 

          doc.flatten(); 

          var docname = fileList[a].name.slice(0,-4); 

          CropStraighten(); 

          doc.close(SaveOptions.DONOTSAVECHANGES);  

          var count = 1; 

          while(app.documents.length){ 

            var saveFile = new File(decodeURI(outfolder) + "/" + docname +"#"+ zeroPad(count,3) + ".jpg"); 

            SaveJPEG(saveFile, 12); 

            activeDocument.close(SaveOptions.DONOTSAVECHANGES) ;  

            count++; 

          } 

         } 

      }; 

      function CropStraighten() { 

      executeAction( stringIDToTypeID('CropPhotosAuto0001'), undefined, DialogModes.NO ); 

      }; 

      function SaveJPEG(saveFile, jpegQuality){ 

      jpgSaveOptions = new JPEGSaveOptions(); 

      jpgSaveOptions.embedColorProfile = true; 

      jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; 

      jpgSaveOptions.matte = MatteType.NONE; 

      jpgSaveOptions.quality = jpegQuality;  

      activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE); 

      function zeroPad(n, s) {  

      n = n.toString();  

      while (n.length < s) n = '0' + n;  

      return n;  

      }; 

       

      Message was edited by: Jeremy Klein

        • 1. Re: How do I crop a percentage from the edges of an image?
          jeremyrklein Level 1

          I think the answer is to change the canvas size of the document to 98% on either side. This works when I do it with a single image. I just don't know how to implement that action into my existing script. Any ideas?

          • 2. Re: How do I crop a percentage from the edges of an image?
            matias.kiviniemi Level 3

            Try this (NOTE, edited existing code without testing, so bugs likely)

             

            function cropPercentage(ratio) {

              if (app.documents.length > 0 && ratio < 0.5) {

              var doc = app.activeDocument

              var crop_w = Math.floor(doc.width*ratio + 0.5)

              var crop_h = Math.floor(doc.height*ratio + 0.5)

              doc.crop(new Array(crop_w, crop_h, doc.width-crop_w, doc.height-crop_h))

              }

            },

            • 3. Re: How do I crop a percentage from the edges of an image?
              jeremyrklein Level 1

              Admittedly, I'm not much of a programmer...though I'm in classes learning about it right now. I tried to plug in your piece of code to my existing, but I get an error for the doc.crop line. Here's what I've got. Any help would be appreciated!

               

              #target Photoshop 

              app.bringToFront; 

              var inFolder = Folder.selectDialog("Please select folder to process");  

              if(inFolder != null){ 

                var fileList = inFolder.getFiles(/\.(jpg|tif|psd|)$/i); 

                var outfolder = new Folder(decodeURI(inFolder) + "/Edited"); 

                if (outfolder.exists == false) outfolder.create(); 

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

                  if(fileList[a] instanceof File){ 

                    var doc= open(fileList[a]); 

                    doc.flatten(); 

                    var docname = fileList[a].name.slice(0,-4); 

                    CropStraighten(); 

                    doc.close(SaveOptions.DONOTSAVECHANGES);  

                    var count = 1; 

                    while(app.documents.length){

                      cropPercentage();

                      var saveFile = new File(decodeURI(outfolder) + "/" + docname +"#"+ zeroPad(count,3) + ".jpg"); 

                      SaveJPEG(saveFile, 12); 

                      activeDocument.close(SaveOptions.DONOTSAVECHANGES) ;  

                      count++; 

                    } 

                  } 

                } 

              }; 

              function CropStraighten() { 

                executeAction( stringIDToTypeID('CropPhotosAuto0001'), undefined, DialogModes.NO ); 

              };

              function cropPercentage(ratio) {

                if (app.documents.length > 0 && ratio < 0.5) {

                  var doc = app.activeDocument

                  var crop_w = Math.floor(doc.width*ratio + 0.5)

                  var crop_h = Math.floor(doc.height*ratio + 0.5)

                  doc.crop(new Array(crop_w, crop_h, doc.width-crop_w, doc.height-crop_h))

                }

              };

              function SaveJPEG(saveFile, jpegQuality){ 

                jpgSaveOptions = new JPEGSaveOptions(); 

                jpgSaveOptions.embedColorProfile = true; 

                jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; 

                jpgSaveOptions.matte = MatteType.NONE; 

                jpgSaveOptions.quality = jpegQuality;  

                activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE); 

              };

              function zeroPad(n, s){  

                n = n.toString();  

                while (n.length < s) n = '0' + n;  

                return n;  

              }; 

              • 4. Re: How do I crop a percentage from the edges of an image?
                matias.kiviniemi Level 3

                You need to give the percentage as parameter to function like cropPercentage(0.02) for 2% (from each side)

                • 5. Re: How do I crop a percentage from the edges of an image?
                  jeremyrklein Level 1

                  Hmm...not sure what is going on. It seems to do something if the percentage is higher than .1, but nothing below that. It is doing some strange things, treating some images, but disregarding others.

                   

                  Thanks for all your help. I can close this thread. I don't want to take up any more of your time and I can always use an automated script to do the process, it just takes longer.

                   

                  Here's is my last version of the code:

                   

                  #target Photoshop

                  app.bringToFront;

                  var inFolder = Folder.selectDialog("Please select folder to process"); 

                  if(inFolder != null){

                    var fileList = inFolder.getFiles(/\.(jpg|tif|psd|)$/i);

                    var outfolder = new Folder(decodeURI(inFolder) + "/Edited");

                    if (outfolder.exists == false) outfolder.create();

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

                      if(fileList[a] instanceof File){

                        var doc= open(fileList[a]);

                        doc.flatten();

                        var docname = fileList[a].name.slice(0,-4);

                        CropStraighten();

                        doc.close(SaveOptions.DONOTSAVECHANGES); 

                        var count = 1;

                        while(app.documents.length){

                          cropPercentage(0.02);

                          var saveFile = new File(decodeURI(outfolder) + "/" + docname +"#"+ zeroPad(count,3) + ".jpg");

                          SaveJPEG(saveFile, 12);

                          activeDocument.close(SaveOptions.DONOTSAVECHANGES) ; 

                          count++;

                        }

                      }

                    }

                  };

                  function CropStraighten() {

                    executeAction( stringIDToTypeID('CropPhotosAuto0001'), undefined, DialogModes.NO );

                  };

                  function cropPercentage(ratio) {

                    if (app.documents.length > 0 && ratio < 0.5) {

                      var doc = app.activeDocument

                      var crop_w = Math.floor(doc.width*ratio + 0.5)

                      var crop_h = Math.floor(doc.height*ratio + 0.5)

                      doc.crop(new Array(crop_w, crop_h, doc.width-crop_w, doc.height-crop_h))

                    }

                  };

                  function SaveJPEG(saveFile, jpegQuality){

                    jpgSaveOptions = new JPEGSaveOptions();

                    jpgSaveOptions.embedColorProfile = true;

                    jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;

                    jpgSaveOptions.matte = MatteType.NONE;

                    jpgSaveOptions.quality = jpegQuality; 

                    activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);

                  };

                  function zeroPad(n, s){ 

                    n = n.toString(); 

                    while (n.length < s) n = '0' + n; 

                    return n; 

                  };