A simple image resize script

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

serous_2010

A simple image resize script

Post by serous_2010 »

I am really new to photoshop scripting. I found a simple script to resize an image - if it's in landscape, the width is changed, if it's in portrait - the height. The only problem is that this script work only for the open document. How can I change the script so that it asks you for a source folder, and then applies the transformation to every single image in all the subfolders?
Mike Hale

A simple image resize script

Post by Mike Hale »

Delete line 53. No, not really. It would help if you posted the script. Otherwise all we can do is make wild guesses.
serous_2010

A simple image resize script

Post by serous_2010 »

Alright, here is the code for resizing an image:

// get a reference to the current (active) document and store it in a variable named "doc"
doc = app.activeDocument;

// change the color mode to RGB. Important for resizing GIFs with indexed colors, to get better results
doc.changeMode(ChangeMode.RGB);

// these are our values for the end result width and height (in pixels) of our image
var fWidth = 500;
var fHeight = 500;

// do the resizing. if height > width (portrait-mode) resize based on height. otherwise, resize based on width
if (doc.height > doc.width) {
doc.resizeImage(null,UnitValue(fHeight,"px"),null,ResampleMethod.BICUBIC);
}
else {
doc.resizeImage(UnitValue(fWidth,"px"),null,null,ResampleMethod.BICUBIC);
}

So, here is my question once again - how do I change the code so that it lets you select an input folder, and then resizes every image in every subfolder? And also, how can I make it save the resized image?
Paul MR

A simple image resize script

Post by Paul MR »

Are you wanting to write this yourself as you haven't said what version of Photoshop, what file types you are working with, if the files should be overwritten or saved with a different name or saved in a different location.
If you want to use a script that is allready written there are a couple of batch processors that should do the job.

Dr. Brown's 1-2-3 Process
http://www.russellbrown.com/scripts.html

Picture Processor
bb/viewtopic.php?f=10&t=3409&sid=0a4f01ce1db 7ea88380985bb222c6db2
serous_2010

A simple image resize script

Post by serous_2010 »

Yes, I would like to write the script myself (or copy it from here if someone else can write it).I have Photoshop cs5, and I'm working with jpg. I would like to save the files in a different folder, and the name should be just a number, starting from 1 and increasing with every new file.
Paul MR

A simple image resize script

Post by Paul MR »

This should be close,I haven't tested it though..
Code: Select all#target photoshop
main();
function main(){
var folders =[];
var topLevel = Folder.selectDialog("Please select top level folder");   
if(topLevel == null ) return;
var outPutFolder = Folder.selectDialog("Please select output folder");
if(outPutFolder == null ) return;
//get a list of all sub folders
folders = FindAllFolders(topLevel, folders);
//add selected foler
folders.unshift(topLevel);
// set up a counter
var Counter = 1;
//iterate through the folder list
for(var f in folders){
   var fileList = folders[f].getFiles("*.jpg");
   for(var j in fileList){
       open(fileList[j]);
// get a reference to the current (active) document and store it in a variable named "doc"
doc = app.activeDocument;
// change the color mode to RGB. Important for resizing GIFs with indexed colors, to get better results
doc.changeMode(ChangeMode.RGB);
// these are our values for the end result width and height (in pixels) of our image
var fWidth = 500;
var fHeight = 500;
// do the resizing. if height > width (portrait-mode) resize based on height. otherwise, resize based on width
if (doc.height > doc.width) {
doc.resizeImage(null,UnitValue(fHeight,"px"),null,ResampleMethod.BICUBIC);
}else {
doc.resizeImage(UnitValue(fWidth,"px"),null,null,ResampleMethod.BICUBIC);
}
var saveFile = new File(outPutFolder+"/"+zeroPad (Counter, 4)+ ".jpg");
//Save document
SaveForWeb(saveFile,80);
//close the document
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
//Increment counter
Counter++;
       }
    }

}
function FindAllFolders( srcFolderStr, destArray) {
   var fileFolderArray = Folder( srcFolderStr ).getFiles();
   for ( var i = 0; i < fileFolderArray.length; i++ ) {
      var fileFoldObj = fileFolderArray;
      if ( fileFoldObj instanceof File ) {         
      } else {
         destArray.push( Folder(fileFoldObj) );
      FindAllFolders( fileFoldObj.toString(), destArray );
      }
   }
   return destArray;
}
function zeroPad(n, s) {
   n = n.toString();
   while (n.length < s)  n = '0' + n;
   return n;
};
function SaveForWeb(saveFile,jpegQuality) {
var sfwOptions = new ExportOptionsSaveForWeb();
   sfwOptions.format = SaveDocumentType.JPEG;
   sfwOptions.includeProfile = false;
   sfwOptions.interlaced = 0;
   sfwOptions.optimized = true;
   sfwOptions.quality = jpegQuality; //0-100
activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);
}
iMatt

A simple image resize script

Post by iMatt »

If you wanted to avoid the scripting exercise you could go to File>Scripts>Image Processor, and that would take care of it. Or you could record a Fit Image action, and run a batch.