Skip to content

Instantly share code, notes, and snippets.

@opdude
Created October 3, 2012 10:45
Show Gist options
  • Select an option

  • Save opdude/3826350 to your computer and use it in GitHub Desktop.

Select an option

Save opdude/3826350 to your computer and use it in GitHub Desktop.
Photoshop script that splits a image up into squares and save the output to files
function callAction(xMax, yMax, width, height, origDoc)
{
var count = 0;
for(var y = 0; y < yMax; y++)
{
for (var x=0; x <xMax; x++) {
SelectAndCopy(x * width, y * height, width, height);
PasteToNewDocument(width, height);
SaveImage(count, origDoc);
CloseDoc();
count++;
}
}
}
function SelectAndCopy(x, y, width, height)
{
var selRegion = Array(Array(x, y),
Array(x + width, y),
Array(x + width, y + height),
Array(x, y + height),Array(x, y));
app.activeDocument.selection.select(selRegion);
app.activeDocument.selection.copy(true);
}
function PasteToNewDocument(width, height)
{
width = new UnitValue(width, 'px');
height = new UnitValue(height, 'px');
var doc = documents.add(width, height, 72, "ScriptCreated", NewDocumentMode.RGB);
doc.paste();
doc.layers.getByName("Background").remove();
app.activeDocument = doc;
}
function CloseDoc()
{
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
function SaveImage(count, origDoc)
{
if (count < 10) {
count = "0" + count;
}
var Name = origDoc.name.replace(/\.[^\.]+$/, '');
var Path = origDoc.path;
var saveFile = File(Path + "/" + Name + count);
if(saveFile.exists) saveFile.remove();
if(selectedType=="Save to PNG") SavePNG(saveFile);
else SaveJPEG(saveFile);
}
function SavePNG(saveFile){
pngSaveOptions = new PNGSaveOptions();
activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);
}
function SaveJPEG(saveFile){
jpegSaveOptions = new JPEGSaveOptions();
jpegSaveOptions.quality = selectedQuality;
activeDocument.saveAs(saveFile, jpegSaveOptions, true, Extension.LOWERCASE);
}
function Dialog()
{
saveAsTypeOptions = [];
saveAsTypeOptions[0] = "Save to PNG";
saveAsTypeOptions[1] = "Save to JPEG";
qualityOptions = [];
for(var i = 12; i >= 1; i--)
{
qualityOptions[i] = i;
}
selectedType = "";
var dlg = new Window ('dialog', 'Select type');
dlg.dropdownlist = dlg.add("dropdownlist", undefined,"");
dlg.quality = dlg.add("dropdownlist", undefined, "");
for (var i=0,len=saveAsTypeOptions.length;i<len;i++)
{
dlg.dropdownlist.add ('item', "" + saveAsTypeOptions[i]);
};
for(var i = 12;i>=1;i--)
{
dlg.quality.add ('item', "" + qualityOptions[i]);
};
dlg.dropdownlist.onChange = function() {
selectedType = saveAsTypeOptions[parseInt(this.selection)];
if(selectedType==saveAsTypeOptions[1]) dlg.quality.show();
else dlg.quality.hide();
};
dlg.quality.onChange = function() {
selectedQuality = qualityOptions[12-parseInt(this.selection)];
};
dlg.add("statictext").text = "Width (px)";
dlg.xSize = dlg.add("edittext");
dlg.xSize.text = 64;
dlg.add("statictext").text = "Height (px)";
dlg.ySize = dlg.add("edittext");
dlg.ySize.text = 64;
var uiButtonRun = "Continue";
dlg.btnRun = dlg.add("button", undefined ,uiButtonRun );
dlg.btnRun.onClick = function() {
this.parent.close(0); };
dlg.orientation = 'column';
dlg.dropdownlist.selection = dlg.dropdownlist.items[0] ;
dlg.quality.selection = dlg.quality.items[0] ;
dlg.center();
dlg.show();
return dlg;
}
function main()
{
if (app.activeDocument == undefined) {
alert("No active Document. Please select one");
return;
}
var count = app.activeDocument.artLayers.length;
var dlg = Dialog();
var firstQ = confirm(selectedType + " may take time depending on the amount of sprites. \n Images will be saved in the same directory as this file.\nContinue?");
if(firstQ) {
var xSize = dlg.xSize.text;
var ySize = dlg.ySize.text;
var xMax = app.activeDocument.width / xSize;
var yMax = app.activeDocument.height / ySize;
callAction(xMax, yMax, xSize, ySize,app.activeDocument);
alert("Process complete");
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment