How can I crop by x,y,width,height all images in a folder, resize them, then save them?

Discipol picture Discipol · Feb 13, 2014 · Viewed 7k times · Source

I am new to Photoshop scripting, but no stranger to Javascript.

I have a folder of images of 1024*1024 that are frames of an animation in a 3d program.

There is only an area at x=54, y=12, width=300, height=234 for all the frames.

After the crop I would like them to be scaled at 65% or whatever I want.

Alternatively I would like the source image to be scaled, the image moved x / y coordinates at 65% reduction so that the outside pixels don't make it in the final product.

There are no psds to speak of, I assume the script would create a blank psd and most likely have it recycled for the batch crop/resize.

Answer

Anna Forrest picture Anna Forrest · Feb 13, 2014

Try something along these lines for the cropping and resizing. You can also copy all the images into a single PSD before you save if that is what you're after, but this sample just saves over the original document. For more info check out your Photoshop JavaScript Reference pdf in your Photoshop install directory.

var dir = new Folder('/c/temp')
var files = dir.getFiles("*.psd");  //change for whatever file type you have

for (var i = 0; i < files.length; i++) {
    var doc = app.open(files[i]);
    var bounds = [54, 12, 354, 246];
    doc.crop(bounds);

    //do the math to figure out how big you want it after resize
    doc.resizeImage(newWidth, newHeight);

    //note this is saving over the original!!!!
    doc.close(SaveOptions.SAVECHANGES)
}