What's new
Photoshop Gurus Forum

Welcome to Photoshop Gurus forum. Register a free account today to become a member! It's completely free. Once signed in, you'll enjoy an ad-free experience and be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Multiple crops in one photo


coen94

New Member
Messages
4
Likes
0
Hello,

I am looking for a solution for cropping multiple (approx. 320) selections in one photo, where I can specify the coordinates for each selection myself. The output must be separate .tif files with a specified file name. Is it possible to use, for example, a csv file as input for the parameters?

Thanks!:)
 
Should be possible, but more information is needed before anyone can help.

How is your CSV file structured? What does this specified filename look like? Is it also part of the CSV file? Where should the TIF files be saved to?...
 
Thanks for the reply,
The CSV file contains 5 values: left, right, top and bottom of the crop and a filename indeed. The tif files must be saved somewhere in a folder.
I am quite familiar with the basics of programming and things like that, so if someone could give an example, I can develop it further by myself and change it to exactly what I'm looking for.
 
Here I have a similar script (had to remove most of the code), you can start with...

You have to change the coords (here the values from the CSV-file are: left, top, right, bottom, filename), add some if statements and the options for the tif-files...

JavaScript:
#target photoshop

selectCSV();

function selectCSV() {
    if ( /Macintosh/i.test( $.os ) ) {
        var csvFile = File.openDialog( 'Select a CSV File', function (f) { return ( f instanceof Folder ) || f.name.match( /\.csv$/i );} );
    } else {
        var csvFile = File.openDialog( 'Select a CSV File', 'File (*.csv):*.csv;' );
    };
    if ( csvFile != null ) { var fileArray = readInCSV( csvFile ) };
    var output = Folder( Folder.desktop + '/Output' );
    if ( ! output.exists ) { output.create(); }
    createOutput( fileArray, output )
};

function createOutput( fileArray, saveFolder ) {
    var userUnits = app.preferences.rulerUnits;
    app.preferences.rulerUnits = Units.PIXELS;
    var doc = app.activeDocument;
    var opts = new TiffSaveOptions();
    opts.embedColorProfile = true;
    opts.imageCompression = TIFFEncoding.TIFFLZW;
    opts.byteOrder = ByteOrder.IBM; //MACOS
    opts.alphaChannels = false;
    opts.layers = false;
    for ( var i = 0; i < fileArray.length; i++ ) {
        var x1 = fileArray[i][0];
        var y1 = fileArray[i][1];
        var x2 = fileArray[i][2];
        var y2 = fileArray[i][3];
        var name = fileArray[i][4];
doc.selection.select([ [x1,y1], [x2,y1], [x2,y2], [x1,y2] ]);

var idCrop = charIDToTypeID( "Crop" );
    var desc36 = new ActionDescriptor();
    var idDlt = charIDToTypeID( "Dlt " );
    desc36.putBoolean( idDlt, true );
executeAction( idCrop, desc36, DialogModes.NO );

            var saveFile = File( saveFolder.fsName + '/' + name + ".tif" );
            doc.saveAs( saveFile, opts, false, Extension.LOWERCASE );
        doc.activeHistoryState = doc.historyStates[0];
    };
//    doc.close( SaveOptions.DONOTSAVECHANGES );
    app.preferences.rulerUnits = userUnits;
};

function readInCSV( fileObj ) {
    var fileArray = new Array();
    fileObj.open( 'r' );
    //fileObj.seek( 0, 0 );
    while( ! fileObj.eof ) {
        var thisLine = fileObj.readln();
        var csvArray = thisLine.split( ',' );
        fileArray.push( csvArray );
    };
    fileObj.close();
    return fileArray;
};
 
I tested the script today and it actually does exactly what I expect it to do. I just had to adjust one tif option. What I also did is try to changing the units from pixels to millimeters. I can't get this working yet, but it's also not necessary.

In the coming weeks I will fill the csv file and see if it works with that many lines, but I assume this will work!

I'll keep you updated!

Thank you very much for the help!! This saves me so much time.
 

Back
Top