Skip to main content
jeremyrklein
Participant
April 14, 2015
Question

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

  • April 14, 2015
  • 2 replies
  • 671 views

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 instanceof File){ 

    var doc= open(fileList); 

    doc.flatten(); 

    var docname = fileList.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

This topic has been closed for replies.

2 replies

matias.kiviniemi
Legend
April 15, 2015

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))

  }

},

jeremyrklein
Participant
April 16, 2015

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 instanceof File){ 

      var doc= open(fileList); 

      doc.flatten(); 

      var docname = fileList.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;  

}; 

matias.kiviniemi
Legend
April 16, 2015

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

jeremyrklein
Participant
April 15, 2015

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?