Skip to main content
Participating Frequently
December 23, 2008
Answered

How can I automate a batch crop and straighten function on single pics?

  • December 23, 2008
  • 2 replies
  • 47826 views
Here's my problem. I have to shoot thousands of pics of baseball cards. The cards are of varying sizes but they're rectangular in shape. I need to crop out the background (black velvet) and maybe straighten the images (depending on how close I can get the images straight when I shoot them)

PS has a nice command that will automatically crop and straighten pics that have been scanned. I've tried to run this command on my single pics and it works fine, except that I also get two additional crops/pics of stray hairs on the black background. I've also tried to make an action that I can turn into a batch or droplet and run on all the pics. I've not had much success. I'll end up with pics of stray hairs and not the card. I then have to go and manually delete the stray pics. Too much work.

Is there any way I can automate the process of cropping, straightening the pics then saving then new version?

Thanks
Correct answer Paul Riggott

I think this was the script....

#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 = doc.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;
};

2 replies

saraa15519875
Participant
June 2, 2021

cropAndStraightenBatch.jsx

 

Script on GitHub.  Save script in text editor as .jsx file. 

Do not place .jsx file in folder with "RUN" scanned photos. 

Do NOT place "OUTPUT" folder in folder with RUN scanned photos.  

Open Photoshop

Go to FILE / SCRIPTS / BROWSE

Select the .jsx file.

You will see popup boxes with instructions in small print at op of box instructing you to make selections.

Wait and watch for the magic to happen.

 

 

Participant
July 29, 2021

Perfect.

 

Only nitpick - the recursion just document that the output folder should not be a sub-folder of the source - otherwise you get in a recursive loop.

 

My project went from weeks to days with this i think.

 

Truly, truly appreciated.

Paul Riggott
Inspiring
December 23, 2008
Participant
May 28, 2011

Hi Paul, looking for the article from the link you posted (http://www.adobeforums.com/webx/.59b7368d/5)

but it takes me to the forum home page (http://forums.adobe.com/index.jspa)

Could you give me some keywords or the topic so I can find it?

Paul Riggott
Paul RiggottCorrect answer
Inspiring
May 28, 2011

I think this was the script....

#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 = doc.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;
};