Skip navigation
Currently Being Moderated

Batch Crop & Straighten

Oct 18, 2009 11:38 AM

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[a] instanceof File){
               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; 
};

 
Replies
  • Currently Being Moderated
    Oct 18, 2009 11:55 AM   in reply to Robdogg

    Here you are, this should do it....

    #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 = 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) + ".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; 
    };
    
    
     
    |
    Mark as:
  • Currently Being Moderated
    Jan 26, 2011 7:40 PM   in reply to Paul Riggott

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 27, 2011 1:24 AM   in reply to mroth5745

    Have a look at this thread.....

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

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 27, 2011 7:59 AM   in reply to mroth5745

    To add more info, crop and straighten is not a script. It is a plug-in. Unlike Adobe scripts such as 'Export Layers To Files', there is no way for an end user to modify a plug-in.

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 27, 2011 8:56 AM   in reply to Robdogg

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 27, 2011 2:07 PM   in reply to Paul Riggott

    I have tried that method, but it does not seem to work for me.  Also, I

    do not think I could automate that process.

     

    Best Regards,

     

    Matthew

     

    MAR Historical

    1140 US HWY 287 #400-104

    Broomfield, CO 80020

    USA

    Phone: 001-303-464-7023

    Fax: 001-303-380-6767

    http://www.marhistorical.com/

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 27, 2011 8:06 PM   in reply to mroth5745

    Maybe you could post a smaple or  a link to a sample so forum members could  better target suggestions for you?

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 5, 2012 1:01 PM   in reply to Paul Riggott

    What a great script. I extended it to recurse through subdirectories, and to filter out images that are too small to be reasonable crops. I hope this helps someone else as much as the original helped me.

     


    #target Photoshop


    // This script recurses through a directory structure and saves straightened/cropped images to

    // a "StraightenedAndCropped" subfolder.

    // The function that does this tends to produce a lot of junk, so

    // this script also filters out images that aren't large enough to

    // be likely candidates.


    // optional (for debugging)

    //$.level = 2

    //$.bp()


    app.bringToFront;

    // you can use a folder selection dialog

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

    // or you can hard-code the folder if you like (making sure to escape the path in Windows)

    //var inFolder = new Folder("C:\\some\\folder");

    if(inFolder != null){

        ProcessFolder(inFolder);

    };


    function ProcessFolder(folder) {

        // process any files here first

        var fileList = folder.getFiles(/\.(jpg|tif|psd|)$/i);


        if (fileList.length > 0) {

            // only create a subfolder etc. if we found anything

            var outfolder = new Folder(decodeURI(folder) + "/StraightenedAndCropped");

            // not sure whether the exists property refreshes on-demand so save an indicator of whether we created the folder

            var created = false;

            //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 = fileList[a].name.slice(0,-4);

                    CropStraighten();

                    var originalWidth = doc.width;

                    var originalHeight = doc.height;

                    doc.close(SaveOptions.DONOTSAVECHANGES);

                    var count = 1;

                    while(app.documents.length){

                        // if the width and  height of the derived image are not both at least 85% of both of the original dimensions,

                        // do not save it at all

                        if (activeDocument.width.value >= (originalWidth.value * .85)

                        && activeDocument.height.value >= (originalHeight.value * .85)) {

                            // create the output folder on demand

                            if ((outfolder.exists == false) && !created) {

                                outfolder.create();

                                created = true;

                            }

                            var saveFile = new File(decodeURI(outfolder) + "/" + docname +"#"+ zeroPad(count,3) + ".jpg");

                            SaveJPEG(saveFile, 12);

                            count++;

                        }

                        activeDocument.close(SaveOptions.DONOTSAVECHANGES) ;

                    }

                }

            }

        }


        // now go looking for new folders, and recurse into them (just don't recurse into our own folders)

        var fileAndFolderList = folder.getFiles();

        for (var i = 0; i < fileAndFolderList.length; i++){

            if (fileAndFolderList[i] instanceof Folder && !endsWith(decodeURI(fileAndFolderList[i]), "StraightenedAndCropped")) {

                ProcessFolder(fileAndFolderList[i]);

            }

        }

    };


    // borrowed from http://stackoverflow.com/questions/280634/endswith-in-javascript

    function endsWith(str, suffix) {

        return str.indexOf(suffix, str.length - suffix.length) !== -1;

    }


    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;

    };

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (1)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points