Photoshop Scripting - Batch Resize Images

on Tuesday, 06 December 2011. Posted in All

If you are like me then you dislike mindless tasks and try to automate anything repetitious. With Microstock you can spend hours resizing and saving your images. What you may not know is that photoshop allows you to write script that automate these tasks.

 

Great Resources:

If you want to write your own scripts I recommend using Adobe's ExtendScript Toolkit If you have the Adobe creative suite installed then you probably already have the Adobe ExtendScript Toolkit Installed. If not, then you can download it for free on the Adobe website.

 

Batch Resize Image Files

Here is a script I didn't write but modified to allow inputs. This script will automatically resize all your images in a folder.

 
/*
* optimizeMyImages for Adobe Photoshop
*
* This script will optimize all images in a given folder and save them to another (or the same) folder.
* Optimization involves resizing, coversion to RGB mode, and saving as a JPEG
* 
* @Copyright: (c) Mohammad Jangda (
 This e-mail address is being protected from spambots. You need JavaScript enabled to view it.
 )
* @License: MIT License (http://www.opensource.org/licenses/mit-license.php)
*
*/
 
// This also allows you to simply double click the JSX file
#target photoshop

/* 
* Config: Change the following values to your desired settings
* MAX_WIDTH: The maximum width (in pixels) allowed for optimized images
* MAX_HEIGHT: The maximum height (in pixels) allowed for optimized images
* FILE_TYPES: The allowed file types that should be optimized
*/
const MAX_WIDTH = prompt("What would you like the maximum width (in pixels) allowed for optimized images?", 1600);  
const MAX_HEIGHT = prompt("What would you like the maximum height (in pixels) allowed for optimized images?", 1200);  
 
var FILE_TYPES = [".jpg",".jpeg",".png",".tiff",".tif",".gif",".psd"];
/*
* end config
*/
 
/*
* begin script
*/
openFilePath = Folder.selectDialog ("Select the folder with your image files");  
saveFilePath = Folder.selectDialog ("Please select a folder to save the web-ready images to:");
 
openFolder = new Folder(openFilePath);
saveFolder = new Folder(saveFilePath);
 
iterate(openFolder, saveFolder);
 
function iterate(openPath, savePath) {
 
  var files = openPath.getFiles();
  var f;
 
  for (var i=0; i<files.length; i++) {
    f = files[i];
    if(f instanceof Folder) {      
      iterate(f, savePath);
    } else if (f instanceof File) {
      //if proper file type, then open resize and save in new location
      if(isAllowedFileType(f)) {          
        app.open(f);
        resizeMyImages(savePath);
      }
    } else {
      alert("Unknown Data Type found. Exiting.")
      exit();
    }
  }
}
 
 
function isAllowedFileType(file) {
  for (var i=0; i<FILE_TYPES.length; i++) {
 
    var filename = file.name.toLowerCase();
    var ext = filename.substr(filename.lastIndexOf('.'));
 
    if(ext == FILE_TYPES[i]) {
      return true;
    }
  }
  return false;
}
 
function resizeMyImages(savePath) { 
  var docs = app.documents;
  var count = docs.length;
  var maxWidth = new UnitValue (MAX_WIDTH, "px");
  var maxHeight = new UnitValue (MAX_HEIGHT, "px");
 
  for (var i = 0; i < count; i++) {
      doc = docs[i];
      fname = doc.name;
    fname = File(savePath + "/" + fname);
    app.activeDocument = doc;
 
      if (doc.width.as("px") < maxWidth.as("px") && doc.height.as("px") < maxHeight.as("px")) {
    // no resize needed
       } else {
        if (doc.width.as("px") > doc.height.as("px")) {
           doc.resizeImage(maxWidth);
        } else {
            ratio = doc.width.as("px") / doc.height.as("px");
            width = maxHeight * ratio;
            doc.resizeImage(width,maxHeight);
        }
    }
 
    //ensure RGB mode
    if(doc.mode!="RGB") {
      doc.changeMode(ChangeMode.RGB);
    }
 
    // Save as JPEG
    jpgSaveOptions = new JPEGSaveOptions();
    jpgSaveOptions.embedColorProfile = true;
    jpgSaveOptions.matte = MatteType.NONE;
    jpgSaveOptions.quality = 8;
    app.activeDocument.saveAs(savePath, jpgSaveOptions, false, Extension.LOWERCASE);
    }
 
    //Close all open images
    while (app.documents.length) {
    app.activeDocument.close();
  }
}
 

Social Bookmarks

Comments (0)

Leave a comment

You are commenting as guest.