// split image into x times y segments and save them as psds; // 2017, use it at your own risk; #target photoshop 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); // the numbers; var theX = 2; var theY = 2; var xFrac = myDocument.width/theX; var yFrac = myDocument.height/theY; // psd options; psdOpts = new PhotoshopSaveOptions(); psdOpts.embedColorProfile = true; psdOpts.alphaChannels = true; psdOpts.layers = true; psdOpts.spotColors = true; // create folder; var folderName = thePath+"/"+theName+"_"+theX+"x"+theY; if (Folder(folderName).exists == false) {Folder(folderName).create()}; //save jpgs; 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.saveAs((new File(folderName+"/"+"_"+theNewName+".psd")),psdOpts,true); theCopy.activeHistoryState = theCopy.historyStates[0]; } }; theCopy.close(SaveOptions.DONOTSAVECHANGES); // reset; app.preferences.rulerUnits = originalUnits; }; ////// buffer 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 ); }; |