Copy link to clipboard
Copied
Hi,
I have a panoramic image that is much longer than higher.
I wish to split in into several squares. I would start from a cropped image in which the number of squares is exact (without any digits after the comma).
How can i automatize this operation?
For example in a 11:1 image i would like to obtain 11 different 1x1 images.
Copy link to clipboard
Copied
I found an old script, which could be useful as a basis...
#target photoshop
var doc = app.activeDocument;
var hs = doc.activeHistoryState;
var width = doc.height.value;
var height = doc.height.value;
var cellWidth = width;
var cellHeight = height;
var options = new ExportOptionsSaveForWeb();
options.format = SaveDocumentType.JPEG;
options.quality = 100;
var doc = app.activeDocument;
var dname = doc.name.substr(0,doc.name.length-4);
var dir = doc.path.toString()+"/";
var rowShift = true;
var imageWidth = activeDocument.width.as('px');
var imageHeight = activeDocument.height.as('px');
var myRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
doc.activeLayer.isBackgroundLayer = false;
doc.resizeCanvas(cellWidth, cellHeight, AnchorPosition.TOPLEFT);
var totalOffset = 0;
var xMovement = 0;
for (var y = 0; y < numberOfRows(); y++)
{
totalOffset = 0;
for (var x = 0; x < numberOfCells(); x++)
{
if (x == 0)
{
xMovement = rowShift;
}
else
{
xMovement = cellWidth;
}
totalOffset += xMovement;
doc.activeLayer.applyOffset(-(xMovement), 0, OffsetUndefinedAreas.WRAPAROUND);
saveCell(x,y);
};
doc.activeLayer.applyOffset(totalOffset, -(cellHeight), OffsetUndefinedAreas.WRAPAROUND);
rowShift = !rowShift;
};
function numberOfCells()
{
var theWidth = imageWidth;
if (rowShift == true)
{
var theWidth = theWidth;
}
return Math.floor(theWidth / cellWidth);
}
function numberOfRows()
{
return Math.floor(imageHeight / cellHeight);
}
function pad(num, size)
{
var s = "000000000" + num;
return s.substr(s.length-size);
}
function saveCell(x, y)
{
var nname = dname + "_" + pad((x + 1), 2);
doc.exportDocument (new File(dir + "/" + nname + ".jpg"), ExportType.SAVEFORWEB, options);
}
app.purge(PurgeTarget.HISTORYCACHES);
doc.activeHistoryState = hs;
app.preferences.rulerUnits = myRulerUnits;
Copy link to clipboard
Copied
And another option here too:
//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.
#target photoshop
/* Start Unsaved Document Error Check - Part A: Try */
unSaved();
function unSaved() {
try {
activeDocument.path;
/* Finish Unsaved Document Error Check - Part A: Try */
/* Main Code Start */
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("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("Enter a whole number:", "3")));
// Convert decimal input to integer
var theYtoInteger = parseInt(theYinput);
// Final result
var theY = theYtoInteger;
// Canvas Division
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 PSD 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.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);
}
alert('PSD Files saved to: ' + folderName);
/* Main Code Finish */
/* Start Unsaved Document Error Check - Part B: Catch */
} catch (err) {
alert('An image must be both open and saved before running this script!')
}
}
/* Finish Unsaved Document Error Check - Part B : Catch */
Copy link to clipboard
Copied
@gtsolid
Did it work or do you still need help?