• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

New Here ,
Apr 14, 2015 Apr 14, 2015

Copy link to clipboard

Copied

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

TOPICS
Actions and scripting

Views

564

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
New Here ,
Apr 14, 2015 Apr 14, 2015

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 14, 2015 Apr 14, 2015

Copy link to clipboard

Copied

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

  }

},

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 15, 2015 Apr 15, 2015

Copy link to clipboard

Copied

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;  

}; 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 15, 2015 Apr 15, 2015

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 16, 2015 Apr 16, 2015

Copy link to clipboard

Copied

LATEST

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 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(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; 

};

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines