Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Cropping multiple Images from single file

Guest
Jul 12, 2009 Jul 12, 2009

Hi,

I have to process a load (several hundreds) of high resolution scanned contact sheets to separate images. The problem with this task is that the images are aranged – and slightly rotated – each time different, so that I have to touch every file. I created an action which offers me a predifined cropping rectangle (to move and/or to rotate), crops this (sub-)image, saves it to a directory and reverts the file to its previous state, to select/crop another subimage. This is more or less what I need, except that the newly created image is overwritten each time again. So instead of saving "subset001_01.tif", "subset001_02.tif" … there is always only one file "subset001_01.tif".

Any ideas how to accomplish this task? Or suggestions how to get PS to save files with incremental naming?

TIA,

Lars

TOPICS
Actions and scripting
7.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guru , Jul 13, 2009 Jul 13, 2009

This should be close to want you need.

// This script will save the active document to a folder with an incremental sufix
// Change the options below to match your needs
var saveFolder = new Folder( '/c/temp2' );
var saveExt = 'tif';
var saveSufixStart = '_';
var saveSufixLength = 3;
tifOpts = new TiffSaveOptions();
tifOpts.embedColorProfile = true;
tifOpts.imageCompression = TIFFEncoding.NONE;
tifOpts.alphaChannels = false;
tifOpts.layers = false;
// End of user options
//===================================

...
Translate
Adobe
Community Expert ,
Jul 13, 2009 Jul 13, 2009

Digressing from Your query: Have You given File – Automate – Crop and Straighten Photos a try?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 13, 2009 Jul 13, 2009

As far as I got to use this function it seemed only to work with single, free standing images. So it would rotate and crop arbitrary parts without any possibility of control. My images consist of several stripes of 120/220 film which are imbedded in either a archive sheet with notes on it or in a contact print frame. In both cases I'll have not only multiple images in different orientation but also a lot of "junk" around the images.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Jul 13, 2009 Jul 13, 2009

This should be close to want you need.

// This script will save the active document to a folder with an incremental sufix
// Change the options below to match your needs
var saveFolder = new Folder( '/c/temp2' );
var saveExt = 'tif';
var saveSufixStart = '_';
var saveSufixLength = 3;
tifOpts = new TiffSaveOptions();
tifOpts.embedColorProfile = true;
tifOpts.imageCompression = TIFFEncoding.NONE;
tifOpts.alphaChannels = false;
tifOpts.layers = false;
// End of user options
//==========================================
function zeroPad ( num, digit ){
   var tmp = num.toString();
   while (tmp.length < digit) { tmp = "0" + tmp;}
   return tmp;
}
var docName = decodeURI ( activeDocument.name );
docName = docName.match( /(.*)(\.[^\.]+)/ ) ? docName = docName.match( /(.*)(\.[^\.]+)/ ) : docName = [ docName, docName, undefined ];
var saveName = docName[ 1 ]; // activeDocument name with out ext
var files = saveFolder.getFiles( saveName + '*.' + saveExt );// get an array of files matching doc name prefix

if( files.length == 0 ) {  // no file with that name so start at one
   var saveNumber = 1;
}
if( files.length == 1 ) { // one file found, see if it has a sufix
   var fileName = decodeURI ( files[ 0 ].name );
   fileName = fileName.match( /(.*)(\.[^\.]+)/ ) ? fileName = fileName.match( /(.*)(\.[^\.]+)/ ) : fileName = [ fileName, fileName, undefined ];
   if( fileName[1].match( /_(\d{3})$/ ) == null ){
      var saveNumber = 1;// does not have sufix so set to one
   } else{// has sufix
      var saveNumber = parseInt( fileName[ 1 ].match( /_(\d{3})$/ )[1] ) + 1; // strip the ext and get the sufix , convert to number and add 1
   }
}
if( files.length > 1 ){
   files.sort();
   var fileName = decodeURI ( files[ files.length -1 ].name );
   fileName = fileName.match( /(.*)(\.[^\.]+)/ ) ? fileName = fileName.match( /(.*)(\.[^\.]+)/ ) : fileName = [ fileName, fileName, undefined ];
   var saveNumber = parseInt( fileName[ 1 ].match( /_(\d{3})$/ )[1] ) + 1; // strip the ext and get the sufix , convert to number and add 1
}
var saveFile = new File( saveFolder + '/' + saveName + '_' + zeroPad( saveNumber, saveSufixLength ) + '.' + saveExt );
activeDocument.saveAs( saveFile, tifOpts ,true ,Extension.LOWERCASE);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 13, 2009 Jul 13, 2009

Great, your script does exactly what I need. Just for the record - my action works now like this:

1. crop with dialog toggled on

2. run script which saves files incremetally to folder

3. select previous history state (to avoid mismatching file names)

I created now several versions of my script for different frame sizes, mapped them to shortcuts and enjoy now the fun of splitting some GB of scans to single images. Thanks again, your script saved me about one week of mind dumbing work. Why PS isn't able to do this via in-built action is another question altogether.

Lars

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 13, 2009 Jul 13, 2009

Why PS isn't able to do this via in-built action is another question altogether.

Well, a lot of people may ask that question with regard to some task they, but not many other people, have to perform regularly.

But the important thing to me seems to be that if one is willing to put some time into it, many things can be achieved/automated  – what use one gets out of Photoshop would seem to depend to a certain degree on what effort one puts into it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Jul 14, 2009 Jul 14, 2009

In a way Photoshop can do this. If you changed your workflow so that you copied each of the images off the contact sheet and minized them until all the images where copied off the sheet you could then batch your save action on the open files and rename them almost anyway you like.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 06, 2009 Aug 06, 2009

I feel like a NOOB asking this considering I've been using Photoshop for well over 10 years now...but...How do I save this script to be usable?  I'm using a Mac with Leopard and Photoshop CS4.

My cousin asked me to crop individual photos out of groupings she scanned in, creating a JPG file with 3-8 pictures per scan.  It seems as though this script will assist me in getting this done faster, but I can't seem to get it saved in the right format, or right extension to even load it through the File > Scripts > Browse menu.  Any help would be extremely appreciated!  Thanks!

William

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 06, 2009 Aug 06, 2009

Paste the code from Michael L Hale’s post into a new file in ExtendScript Toolkit (part of Photoshop’s installation, Applications/Utilities/Adobe Utilities/ExtendScript Toolkit CS4) and save it as a jsx-file into Photoshop’s Presets/Scripts-folder.

After restarting Photoshop the Script should be available under File > Scripts and can be assigned a Keyboard Shortcut directly, recorded into an Action or (in CS4) be used in a Configurator-Panel.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Aug 07, 2009 Aug 07, 2009

The script as is saves the document as a tiff file. If you would rather have it save as jpeg let me know and I will post a jpeg version.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 10, 2009 Aug 10, 2009

a JPG version of this script would be excellent.  Please post...it would be much appreciated.  Thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Aug 11, 2009 Aug 11, 2009

// This script will save the active document to a folder with an incremental sufix
// Change the options below to match your needs
var saveFolder = new Folder( '/c/temp2' );
var saveSufixStart = '_';
var saveSufixLength = 3;
saveOptions = new JPEGSaveOptions();
saveOptions.embedColorProfile = true;
saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
saveOptions.matte = MatteType.NONE;
saveOptions.quality = 8;
// End of user options
//==========================================
var saveExt = 'jpg';
function zeroPad ( num, digit ){
   var tmp = num.toString();
   while (tmp.length < digit) { tmp = "0" + tmp;}
   return tmp;
}
var docName = decodeURI ( activeDocument.name );
docName = docName.match( /(.*)(\.[^\.]+)/ ) ? docName = docName.match( /(.*)(\.[^\.]+)/ ) : docName = [ docName, docName, undefined ];
var saveName = docName[ 1 ]; // activeDocument name with out ext
var files = saveFolder.getFiles( saveName + '*.' + saveExt );// get an array of files matching doc name prefix

if( files.length == 0 ) {  // no file with that name so start at one
   var saveNumber = 1;
}
if( files.length == 1 ) { // one file found, see if it has a sufix
   var fileName = decodeURI ( files[ 0 ].name );
   fileName = fileName.match( /(.*)(\.[^\.]+)/ ) ? fileName = fileName.match( /(.*)(\.[^\.]+)/ ) : fileName = [ fileName, fileName, undefined ];
   if( fileName[1].match( /_(\d{3})$/ ) == null ){
      var saveNumber = 1;// does not have sufix so set to one
   } else{// has sufix
      var saveNumber = parseInt( fileName[ 1 ].match( /_(\d{3})$/ )[1] ) + 1; // strip the ext and get the sufix , convert to number and add 1
   }
}
if( files.length > 1 ){
   files.sort();
   var fileName = decodeURI ( files[ files.length -1 ].name );
   fileName = fileName.match( /(.*)(\.[^\.]+)/ ) ? fileName = fileName.match( /(.*)(\.[^\.]+)/ ) : fileName = [ fileName, fileName, undefined ];
   var saveNumber = parseInt( fileName[ 1 ].match( /_(\d{3})$/ )[1] ) + 1; // strip the ext and get the sufix , convert to number and add 1
}
var saveFile = new File( saveFolder + '/' + saveName + '_' + zeroPad( saveNumber, saveSufixLength ) + '.' + saveExt );
activeDocument.saveAs( saveFile, saveOptions ,true ,Extension.LOWERCASE);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 12, 2013 Jul 12, 2013

Thanks Michael this was a life saver XD I changed it for PNG and figured out the folder system for a Mac which is basically the same for windows just changed the starting dir. Oh and I'm using this in Adobe Photoshop CS5. If you don't want to read above ^ Copy the code into applications>utilities>adobe utilities - C5>ExtendScript Toolkit.app

Then save it somewhere where you can find it. Then in photoshop with your image open go file>scripts>browse and find where you saved the script and select it.

***Hmmm is only letting me save up to 8 in one folder then cut them over and start again.

// This script will save the active document to a folder with an incremental sufix

// Change the options below to match your needs

var saveFolder = new Folder( '/Users/name/foldername' ); //don't put the ending "/" this is for mac folders windows is /c/foldername

var saveSufixStart = '_';

var saveSufixLength = 3;

saveOptions = new PNGSaveOptions();

saveOptions.embedColorProfile = true;

saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;

saveOptions.matte = MatteType.NONE;

saveOptions.quality = 8;

// End of user options

//==========================================

var saveExt = 'png';

function zeroPad ( num, digit ){

   var tmp = num.toString();

   while (tmp.length < digit) { tmp = "0" + tmp;}

   return tmp;

}

var docName = decodeURI ( activeDocument.name );

docName = docName.match( /(.*)(\.[^\.]+)/ ) ? docName = docName.match( /(.*)(\.[^\.]+)/ ) : docName = [ docName, docName, undefined ];

var saveName = docName[ 1 ]; // activeDocument name with out ext

var files = saveFolder.getFiles( saveName + '*.' + saveExt );// get an array of files matching doc name prefix

if( files.length == 0 ) {  // no file with that name so start at one

   var saveNumber = 1;

}

if( files.length == 1 ) { // one file found, see if it has a sufix

   var fileName = decodeURI ( files[ 0 ].name );

   fileName = fileName.match( /(.*)(\.[^\.]+)/ ) ? fileName = fileName.match( /(.*)(\.[^\.]+)/ ) : fileName = [ fileName, fileName, undefined ];

   if( fileName[1].match( /_(\d{3})$/ ) == null ){

      var saveNumber = 1;// does not have sufix so set to one

   } else{// has sufix

      var saveNumber = parseInt( fileName[ 1 ].match( /_(\d{3})$/ )[1] ) + 1; // strip the ext and get the sufix , convert to number and add 1

   }

}

if( files.length > 1 ){

   files.sort();

   var fileName = decodeURI ( files[ files.length -1 ].name );

   fileName = fileName.match( /(.*)(\.[^\.]+)/ ) ? fileName = fileName.match( /(.*)(\.[^\.]+)/ ) : fileName = [ fileName, fileName, undefined ];

   var saveNumber = parseInt( fileName[ 1 ].match( /_(\d{3})$/ )[1] ) + 1; // strip the ext and get the sufix , convert to number and add 1

}

var saveFile = new File( saveFolder + '/' + saveName + '_' + zeroPad( saveNumber, saveSufixLength ) + '.' + saveExt );

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

Thanks again Michael XD

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Jul 13, 2013 Jul 13, 2013

I am not sure why this doesn't work for you. The only thing I can think might be a problem is if the case of the name or extension makes a difference on Mac. It doesn't on Windows. So maybe getFiles is not getting all the matching files. And it just happens that the case difference is with the 9 file. Otherwise I don't see why it would start over after the 8 file.

You can add an alert(files.sort()); line to see if it is finding all the files you expect it to find.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 14, 2013 Jul 14, 2013

Yes it finds all of the files just doesn't increment on 8 to become 9 for some reason.

I inserted in alert(files.sort()); alert(files.length); on line 43 and files.sort alerts all the files till #8 and files.length alerts 8. I don't know this programming language good enough to find out why its not adding 1 at 8. But I guess its in the line 28 block because I put the alert in that if statement and it doesn't show so for some reason that if is returning false. I'm guessing "( /_(\d{3})$/ )" is stating characters and maybe its missing 8 or 9??

It's ok if you don't have time for this I just move the images over into a new folders every 8 and then just batch new file names so I can have them all in one folder.

Thanks again for making this script.

Edit*** Ya actually I just noticed it saves over number 1 once it finds 8

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Jul 14, 2013 Jul 14, 2013

It's ok if you don't have time for this...

It's more a matter of I can't reproduce that behavior. Can you post the filenames you are working with that don't increment correctly?

What /_(\d{3})$/ means it match three digits after an underscore at the end of the filename. From what your reported was in the alert the problem isn't with that line. The problem is getFiles() is not getting all the files it should be getting.

EDIT: see if this works any better

// This script will save the active document to a folder with an incremental sufix
// Change the options below to match your needs
var saveFolder = new Folder( '~/desktop/test' ); //don't put the ending "/" this is for mac folders windows is /c/foldername
var saveSufixStart = '_';
var saveSufixLength = 3;
var saveExt = 'png';
var saveOptions = new PNGSaveOptions();
saveOptions.embedColorProfile = true;
saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
saveOptions.matte = MatteType.NONE;
saveOptions.quality = 8;

// End of user options

//==========================================
function zeroPad ( num, digit ){
   var tmp = num.toString()
;   while (tmp.length < digit) {
tmp = "0" + tmp;}   return tmp
;}
var searchRE = new RegExp(saveSufixStart+'(\\d{'+saveSufixLength+'})$');
var docName = decodeURI ( activeDocument.name );docName = docName.match( /(.*)(\.[^\.]+)/ ) ? docName = docName.match( /(.*)(\.[^\.]+)/ ) : docName = [ docName, docName, undefined ];
var saveName = docName[ 1 ]; // activeDocument name with out ext
var RE = new RegExp(saveName+'.*\.'+saveExt);
var files = saveFolder.getFiles( RE );// get an array of files matching doc name prefix
if( files.length == 0 ) {  // no file with that name so start at one  
    var saveNumber = 1;
}
if( files.length == 1 ) { // one file found, see if it has a sufix 
    var fileName = decodeURI ( files[ 0 ].name );
    fileName = fileName.match( /(.*)(\.[^\.]+)/ ) ? fileName = fileName.match( /(.*)(\.[^\.]+)/ ) : fileName = [ fileName, fileName, undefined ];
    if( fileName[1].match( searchRE) == null ){
        var saveNumber = 1;// does not have sufix so set to one  
    } else{// has sufix    
        var saveNumber = parseInt( fileName[ 1 ].match( searchRE )[1] ) + 1; // strip the ext and get the sufix , convert to number and add 1 
    }
}
if( files.length > 1 ){
   files.sort();  
   var fileName = decodeURI ( files[ files.length -1 ].name );
   fileName = fileName.match( /(.*)(\.[^\.]+)/ ) ? fileName = fileName.match( /(.*)(\.[^\.]+)/ ) : fileName = [ fileName, fileName, undefined ];
   var saveNumber = Number( fileName[ 1 ].match(searchRE)[1] ) + 1; // strip the ext and get the sufix , convert to number and add 1
}
var saveFile = new File( saveFolder + '/' + saveName + '_' + zeroPad( saveNumber, saveSufixLength ) + '.' + saveExt );
activeDocument.saveAs( saveFile, saveOptions ,true ,Extension.LOWERCASE);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 14, 2013 Jul 14, 2013

Hey that got past 8 XD thanks so much!

again for people with macs

in this line

var saveFolder = new Folder( '~/desktop/test' ); //don't put

change "~/desktop/test"

to     "/Users/username/Desktop/foldername"


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Jul 14, 2013 Jul 14, 2013

I think if you replace parseInt with Number in the two lines that use that method it should work correctly. I forgot to change it in the files.length == 1 if statement.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Jul 14, 2013 Jul 14, 2013

new Folder('~/desktop') works on both Windows and Mac. The '~' char is a shortcut to the users folder and works cross platform. See the JavaScript Tools Guide for details about working with files and folders with ExtendScript.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 14, 2013 Jul 14, 2013

Ya you're right, thanks again for all your help.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 22, 2014 Nov 22, 2014
LATEST

Hey Michael, sorry for bothering you on almost 2 year old post...

Any way to have structure like following, that'll actually work?
( saveFolder + '/' + 'Icon_' + zeroPad( saveNumber, saveSufixLength ) + '_@2x' +'.' + saveExt ); 

Adding a second custom suffix seams to break the order...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines