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
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?
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))
}
},
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;
};
Copy link to clipboard
Copied
You need to give the percentage as parameter to function like cropPercentage(0.02) for 2% (from each side)
Copy link to clipboard
Copied
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;
};