Auto Image Resizing via Photoshop Scripting

I recently needed to process all .png and .tga files in an entire folder structure including sub folders. The process was pretty simple and using python via the PIL module (for .pngs) and Image Magick (for .tga) I simply took any images found and made half resolution and quarter resolution versions (effectively MipMaps, but not stored in a single image). Problem is that we are still generating textures for other scenes/shots and so I decided to automate this ‘mipmap’ task via photoshop scripts for the texture artists. So after a mini crash course in JavaScript & reading the Photoshop Scripting Guide this is what I came up with. Honestly, probably not the cleanest code… but it does the job and that’s what counts when you’re mid production.

The Scipt

this version saves out 32bit TGA files with their alpha channels

//set up
preferences.rulerUnits = Units.PERCENT
displayDialogs = DialogModes.NO

//Doc Info
var docRef = app.documents[0]
var typeLayer = app.activeDocument.artLayers.add();
typeLayer.name = app.activeDocument.name.slice(0,-4);

// Save as a new TGA file with these options.
var Options = new TargaSaveOptions();
Options.alphaChannels = true;
Options.resolution = TargaBitsPerPixels.THIRTYTWO;
Options.rleCompression = false;

//make HalfRes
var resize = docRef.resizeImage( 50,50 )
var saveFile = new File(decodeURI(activeDocument.fullName.fsName).slice(0,-4) + "_med.tga");
app.activeDocument.saveAs (saveFile, Options, false, Extension.LOWERCASE);

//make QuaterRes
var resize = docRef.resizeImage( 50,50 )
var saveFile = new File(decodeURI(activeDocument.fullName.fsName).slice(0,-4) + "_low.tga");
app.activeDocument.saveAs (saveFile, Options, false, Extension.LOWERCASE);

//close without saving
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

a modified version by simply changing the save options & file extensions allows you to just about any format, we also use the PNG format so therefore

// Save as a new PNG file with these options.
var options = new PNGSaveOptions();

is switched out to save PNGs.

// Save as a new TIF file with these options.
var Options = new TiffSaveOptions();
Options.alphaChannels = true;

is switched out to save TIFs.

Download Scripts

I have put them both into a Zip File with instructions on how to install and use, and that is Downloadable Here!!

Enjoy!!

~C 😀

Tweet about this on TwitterShare on FacebookShare on Google+Share on TumblrPin on Pinterest

1 Comment

  1. john says:

    hi Chris
    this is jonathan chen from Taiwan, we have telcine + Davinci…
    working on film / video / and archiving stuff,
    ur article inspire me a lot, drop a email to say thanks,

Leave a Reply