Photoshop Script to split large images into smaller ones

Here’s the code:
var Tiler = function (width, height, tileSize, levels) {
this.width = width;
this.height = height;
this.tileSize = (typeof tileSize === "undefined") ? 256 : tileSize;
this.levels = (typeof levels === "undefined") ? 2 : levels;
this.cols = Math.ceil(width/this.tileSize);
this.rows = Math.ceil(height/this.tileSize);
};
// Set Adobe Photoshop CS5 to use pixels.
var initialPrefs = app.preferences.rulerUnits; // will restore at end
app.preferences.rulerUnits = Units.PIXELS;

var jpgOpts = new JPEGSaveOptions();
jpgOpts.embedColorProfile = false;
jpgOpts.formatOptions = FormatOptions.OPTIMIZEDBASELINE;
jpgOpts.quality = 12;

var doc = app.activeDocument;
var tiler = new Tiler(doc.width, doc.height, 256);
var outputPath = "F:/Spliced Files/";
app.displayDialogs = DialogModes.NO;


//alert(tiler.cols + " : " + tiler.rows);

var limit = 10;

//tiler.cols = 1;
//tiler.rows = 5;
var newDoc = app.documents.add (256,256,72);

for(var i = 0; i < tiler.cols; i++)
{
for (var j=0; j<tiler.rows; j++)
{
app.activeDocument = doc;

var x1 = i * tiler.tileSize;
var x2 = x1 + tiler.tileSize;
var y1 = j * tiler.tileSize;
var y2 = y1 + tiler.tileSize;

var shapeRef = [[x1,y1], [x1,y2],[x2,y2],[x2,y1]];

doc.selection.select(shapeRef);
doc.selection.copy(true);

app.activeDocument = newDoc;

newDoc.paste();
newDoc.saveAs(new File(outputPath + i + "-" + j + ".jpg"),jpgOpts, true, Extension.LOWERCASE);

var layer = app.activeDocument.activeLayer;
layer.remove();

}
}

Leave a Comment

Your email address will not be published. Required fields are marked *