Conditional Resize Script Advice

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Tom, Kukurykus

steen

Conditional Resize Script Advice

Post by steen »

I'm looking into creating a script that will crawl through a folder (and subfolders) and resize all of the images it finds based on the current images size (ie. if the image is over a certain size, uniformly scale it down). The initial images may be in a variety of formats, but will be output as 8bit max quality jpegs. The folders may also contain non-image files, so it has to ignore them. I have very limited scripting experience, but I'm pretty familiar with as3 so java script isn't all that scary to me. There are a few ways I can think of to do this, but it's important that the script be portable between windows machines, and be compatible with other recent versions of photoshop if possible.

I'm currently getting hung up on how I should go about crawling through the folders. I thought about using image processor with an action that contains a script, but that seems to tie it to my specific machine. If I use a script to control the whole thing, I'm not sure how to go about telling it which files to ignore and which to process. I appreciate any advice or thoughts!

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

H.N.Orange

Conditional Resize Script Advice

Post by H.N.Orange »

1. Will the top most folder be the same every time, or will the user potentially be selecting a new one each time the script is run?
2. What is the "certain size" condition that triggers the down sizing, I assume it's a pixel value?
3. Are you down sizing by a percentage, or to a hard number (e.g 50% or 600px )?
4. Do you want the longest side to be the limit, or the shortest side (if an image is 1000px X 900px and you want to be under 500px, should the image end up as 500px X 450px, or 556px X 500px)?
5. Where will the files be saved to (usually in everyone's best interest to be non-destructive with the source files)? Are you overwriting(not recommended)?
6. Will the new files be saved with a new name/suffix/prefix?
H.N.Orange

Conditional Resize Script Advice

Post by H.N.Orange »

This'll get you through the folders :


Code: Select all   

#target Photoshop
//2011, developed in CS2, use at your own risk.

if (app.documents.length){
   alert ("Please close any open documents and run this script again.");
}else{
   main();
}


function main(){
//////////////VARIABLES and ARRAY DECLARATIONS////////////////////
   var myFolder = Folder(Put Path Here);//put a path to your top folder here (in this format- "C:\\Documents\\etc)
   var fileTypes = (/\.(jpg|tga|tif|psd|)$/i);//add file types here if you need more (follow the convention!)
   var maxDimension = UnitValue(500, 'px');//define the maximum pixel dimension before downsizing
   app.preferences.rulerUnits = Units.PIXELS;  //we'll use pixel dimensions
   files =[];
   folders = [];
   folders.push(myFolder);
   completeList = [folders, files];

   ////////////////////LOGIC////////////////////
   for(var index = 0;index<completeList[0].length;index++){
      completeList = findNested(folders, completeList, index);
   }
   //alert ("folders: "+completeList[0]+"\nfiles: "+completeList[1]);//commented out for now, uncomment if you want to see what's being found


   ////////////////////FUNCTIONS////////////////////
   function findNested(folderIn, arrayIn, index){
      tempItem = [];
      tempItem = folderIn[index].getFiles();
      for (var a=0;a<tempItem.length;a++){
         if (typeof tempItem[a].open == "undefined"){
            arrayIn[0].push(tempItem[a]);
         }else{
            if(tempItem[a].fsName.toString().match(fileTypes)){
               arrayIn[1].push(tempItem[a]);
            }//if
         }//if/else   
      }//for
      return arrayIn;
   }//findNested

}//main
steen

Conditional Resize Script Advice

Post by steen »

Excellent. Thanks so much for the reply. I had some time to read through the forum a little more since my initial post and found an example of how to get the file names into an array. It works pretty much the same way yours does as far as I can tell.

The images will be sized to fit into a 954w X 768h window, so the script will have to check the size of the image and then take the appropriate action for a wide or tall image.

The images will overwrite in the case of a file with the same extension. The folders will be duplicated to begin with, so it's not an issue that it's destructive. I may even have it remove the original files once the images are saved as jpg. The name will be the same as the original.

Thanks again for your assistance!
H.N.Orange

Conditional Resize Script Advice

Post by H.N.Orange »

steen wrote:Excellent. Thanks so much for the reply. I had some time to read through the forum a little more since my initial post and found an example of how to get the file names into an array. It works pretty much the same way yours does as far as I can tell.



Can you link me to this?
I'd like to compare and contrast

-Orange