Skip to main content
Participant
October 18, 2009
Question

Batch Crop & Straighten

  • October 18, 2009
  • 3 replies
  • 10145 views

A while ago a good chap helped me out by creating a script that prompted the user for a folder, then ran the Crop and Straighten Photos command on all images contained in the folder and saved the newly created photos in a new folder.  The script is below.

The script saves cropped photos as PNGs, I'd like to save them as JPEG.  Could someone help me out by tweaking the script to save as JPEG?  I'd like them saved with the highest quality option.

#target Photoshop

app.bringToFront;

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

if(inFolder != null){

     var fileList = inFolder.getFiles(/\.(jpg|tif|png|)$/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) + ".png");

                    SavePNG(saveFile);

                    activeDocument.close(SaveOptions.DONOTSAVECHANGES) ;

                    count++;

               }

          }

     }

};

function CropStraighten() {

     function cTID(s) { return app.charIDToTypeID(s); };

     function sTID(s) { return app.stringIDToTypeID(s); };

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

};

function SavePNG(saveFile){

    pngSaveOptions = new PNGSaveOptions();

    pngSaveOptions.embedColorProfile = true;

    pngSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;

    pngSaveOptions.matte = MatteType.NONE;

    pngSaveOptions.quality = 1;

     pngSaveOptions.PNG8 = false; //24 bit PNG

    pngSaveOptions.transparency = true;

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

}

function zeroPad(n, s) {

     n = n.toString();

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

     return n;

};

This topic has been closed for replies.

3 replies

Participant
November 7, 2023

Just a heads up, I tried to run the script but it didn't work. I discovered the script seems to have errors in it. This line for example is wrong:

               var doc= open(fileList);

This line says "open an array of files". The open command doesn't work like that though. It can only open one file.

 

In fact, the script fails before this, at this line:

          if(fileList instanceof File){

This line says "if my list of files is a file, then carry on". A list of files isn't a file, so the script skips everything after this.

 

So I fixed the script so it works now. See below. It works on a folder of images. Just make sure there are no subfolders in the folder, otherwise the script will give an error. 

#target Photoshop
app.bringToFront;
var inFolder = Folder.selectDialog("Please select folder to process");
var outfolder = inFolder;
if(inFolder != null){
     var fileList = inFolder.getFiles();
     for(var a = 0 ;a < fileList.length; a++)
     {
          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) + ".png");
               SavePNG(saveFile);
               activeDocument.close(SaveOptions.DONOTSAVECHANGES);
               count++;
          }
     }
};
function CropStraighten() 
{
     function cTID(s) { return app.charIDToTypeID(s); };
     function sTID(s) { return app.stringIDToTypeID(s); };
     executeAction( sTID('CropPhotosAuto0001'), undefined, DialogModes.NO );
};
function SavePNG(saveFile)
{
    pngSaveOptions = new PNGSaveOptions();
    pngSaveOptions.embedColorProfile = true;
    pngSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
    pngSaveOptions.matte = MatteType.NONE;
    pngSaveOptions.quality = 1;
    pngSaveOptions.PNG8 = false; //24 bit PNG
    pngSaveOptions.transparency = true;
    activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);
}
function zeroPad(n, s) 
{
     n = n.toString();
     while (n.length < s) n = '0' + n;
     return n;
};

 

January 27, 2011

The approaches mentioned in the link provided by Paul are pretty good.  Thanks for sharing.  The procedure below (duplicated from another similar post I made) may help automate the process a little more.  This approach does assume that there is at least 1 pixel more around your image than the 30(or whatever) pixels you want to add to the image(s).  This is done as an action

- Select entire image
- Select Border with 1 pixel
- Select Grow
At this point a selection should be around your image(s)  with no border.
- Invert Selection

- Expand Selection by number of pixels for desired border extension

- Cmd+J (Ctrl+J for PCs) to copy just the selections to new layer

- File>Automate>Crop & straighten

That should crop and staighten the image(s) to individual image(s) including the extended border.

If you are using a script, you can either call the script from the action or call the action from the script.  Hope that helps.

Participant
January 27, 2011

Is there a way to adjust the crop and straighten script to allow for a border of say 30 pixels to be buffered around the image?  I need something like this for work that I am doing.  The current function (in my CS3) crops too tight in instances and give an incorrect impression of my items.  I need to be albe to use the new fuction in an automated action.

Thanks,

Matthew

Paul Riggott
Inspiring
January 27, 2011

Have a look at this thread.....

http://forums.adobe.com/thread/778507?tstart=0