@defaultmwout241jqs0 wrote:
I'm glad I found this script, but I would also like to adjust for the Export/Save for Web as PNG. Can you post your solution?
EDIT:
Here you go! As you didn't state your requirements, you can change the pngOptions as required...
//community.adobe.com/t5/photoshop/dividing-big-document-to-smaller-ones/m-p/9311087
// split image into x times y segments and save them as psds;
// 2017, use it at your own risk;
// 2020 - Modified by Stephen Marsh, adding prompts, save alert, saved doc test.
// 2021 - Modified by Stephen Marsh, changed output from PSD to Export Save for Web: PNG
#target photoshop
mainFunction();
function mainFunction() {
try {
activeDocument.path;
if (app.documents.length > 0) {
var originalUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// Document;
var myDocument = app.activeDocument;
var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
var thePath = myDocument.path;
var theCopy = myDocument.duplicate("copy", false);
// Loop the X input prompt until a number is entered
var theXinput;
while (isNaN(theXinput = prompt("(X) - Enter a whole number:", "3")));
// Convert decimal input to integer
var theXtoInteger = parseInt(theXinput);
// Final result
var theX = theXtoInteger;
// Loop the Y input prompt until a number is entered
var theYinput;
while (isNaN(theYinput = prompt("(Y) - Enter a whole number:", "3")));
// Convert decimal input to integer
var theYtoInteger = parseInt(theYinput);
// Final result
var theY = theYtoInteger;
var splitCount = theX * theY;
alert("The open image will be split into " + splitCount + " tiles and exported as PNG files...");
// Canvas Division
var xFrac = myDocument.width / theX;
var yFrac = myDocument.height / theY;
// PNG Options
var pngOptions = new ExportOptionsSaveForWeb();
pngOptions.PNG8 = false;
pngOptions.transparency = true;
pngOptions.interlaced = false;
pngOptions.quality = 100;
pngOptions.includeProfile = false;
pngOptions.format = SaveDocumentType.PNG;
// Create folder;
var folderName = thePath + "/" + theName + "_" + theX + "x" + theY;
if (Folder(folderName).exists === false) {
Folder(folderName).create()
}
// Export Save for Web PNG files;
for (var n = 1; n <= theY; n++) {
for (var m = 1; m <= theX; m++) {
cropTo((m - 1) * xFrac, (n - 1) * yFrac, m * xFrac, n * yFrac);
var theNewName = theName + "_" + bufferNumberWithZeros(m, 3) + "_" + bufferNumberWithZeros(n, 3);
theCopy.exportDocument((File(folderName + "/" + "_" + theNewName + ".png")), ExportType.SAVEFORWEB, pngOptions);
theCopy.activeHistoryState = theCopy.historyStates[0];
}
}
theCopy.close(SaveOptions.DONOTSAVECHANGES);
// Reset Original Ruler Units
app.preferences.rulerUnits = originalUnits;
}
// Pad Number with zeros
function bufferNumberWithZeros(number, places) {
var theNumberString = String(number);
for (var o = 0; o < (places - String(number).length); o++) {
theNumberString = String("0" + theNumberString)
}
return theNumberString
}
// Crop
function cropTo(x1, y1, x2, y2) {
// =======================================================
var desc7 = new ActionDescriptor();
var desc8 = new ActionDescriptor();
var idPxl = charIDToTypeID("#Pxl");
desc8.putUnitDouble(charIDToTypeID("Top "), idPxl, y1);
desc8.putUnitDouble(charIDToTypeID("Left"), idPxl, x1);
desc8.putUnitDouble(charIDToTypeID("Btom"), idPxl, y2);
desc8.putUnitDouble(charIDToTypeID("Rght"), idPxl, x2);
var idRctn = charIDToTypeID("Rctn");
desc7.putObject(charIDToTypeID("T "), idRctn, desc8);
desc7.putUnitDouble(charIDToTypeID("Angl"), charIDToTypeID("#Ang"), 0.000000);
desc7.putBoolean(charIDToTypeID("Dlt "), false);
desc7.putEnumerated(stringIDToTypeID("cropAspectRatioModeKey"), stringIDToTypeID("cropAspectRatioModeClass"), stringIDToTypeID("pureAspectRatio"));
desc7.putBoolean(charIDToTypeID("CnsP"), false);
executeAction(charIDToTypeID("Crop"), desc7, DialogModes.NO);
}
alert(splitCount + ' PNG Files saved to: ' + folderName);
} catch (err) {
alert('An image must be both open and saved before running this script!')
}
}
Thank you!
That worked as I hoped.
You chose the png options I wanted, too.
I appreciate your help!