Skip to main content
Tom114K
Known Participant
December 12, 2021
Question

Script for splitting multiple PSDs to defined slices

  • December 12, 2021
  • 3 replies
  • 4750 views

Hi guys,

I need to export data-driven graphics into slices. As far as I know, it is not possible to do that in Photoshop (I use CS5), so I would like to do this step-by-step.

 

My question: Is it possible to batch-split PSDs to multiple PSDs according to defined slices via script? Or, is it possible to batch-split existing JPGs to multiple JPGs (the slices are just dividing one document to two of them vertically, evenly spaced, so this approach might just be sufficient)? The process to solve my problem would be following:

 

1. Export Data Sets as Files (this would create tens of PSDs according to my data set)
2. Launch some script to split exported PSDs according to slices (out format: PSDs)

3. File > Scripts > Image Processor to convert PSDs to JPGs

 

OR

 

1. Export Data Sets as Files (this would create tens of PSDs according to my data set)

2. File > Scripts > Image Processor to convert PSDs to JPGs.
3. Launch some script to split JPGs according to predefined dimensions 

 

Thank you for your advice.

 

This topic has been closed for replies.

3 replies

Stephen Marsh
Adobe Expert
December 14, 2021

The following script can be recorded into a single step action, then the action can be used with the batch command.

 

Change the following as required to suit your desired column/rows layout (it does not use slices or guides):

 

var theX = 4;
var theY = 2;

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/slicing-a-very-image-24k-px-x-34k-px-into-smaller-crops/m-p/9366573

 

/* https://community.adobe.com/t5/photoshop-ecosystem-discussions/slicing-a-very-image-24k-px-x-34k-px-into-smaller-crops/m-p/9366573#M115021 */

// split image into x times y segments and save them as jpgs;
// 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", true);
// the numbers;
var theX = 4;
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;
// jpg options;
var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = 9;
jpegOptions.embedColorProfile = true;
jpegOptions.matte = MatteType.NONE;
// 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+".jpg")),jpegOptions,true);
theCopy.activeHistoryState = theCopy.historyStates[0];
//executeAction( charIDToTypeID("undo"), undefined, DialogModes.NO );
}
};
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 );
};

 

Code taken from this topic:

https://community.adobe.com/t5/photoshop-ecosystem-discussions/slicing-a-very-image-24k-px-x-34k-px-into-smaller-crops/m-p/9366573

 

Different code that does the same thing:

https://www.andrewnoske.com/wiki/Adobe_Photoshop_-_Scripts_Related_to_Montaging

 

Another topic with related info using slices or guides to layers (not files):

https://community.adobe.com/t5/photoshop-ecosystem-discussions/divide-my-image-to-layers/m-p/12467039

 

Instructions for saving/installing code:

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

 

Tom114K
Tom114KAuthor
Known Participant
December 14, 2021

Thank you very much, I will test that out.

Stephen Marsh
Adobe Expert
December 12, 2021

Slices would appear to be unnecessary as this is just a case of two crops via canvas size adjustment.

Tom114K
Tom114KAuthor
Known Participant
December 12, 2021

This is just an example. The real situation is much more complex.

Tom114K
Tom114KAuthor
Known Participant
December 13, 2021

Yes, that is one option – it would depend on the slice layout which you haven't shared yet.

 

If the slice layout produced 8 evenly distributed areas, there are scripts than can divide up an image into "tiles" and save them out.

 

However, if the slices produced irregular sized areas, then a selection based crop would probably be better.

 

There is still the possibility of scripting this, though at least in the short term it would appear to be more pragmatic to look into this as batch action.


I will stick with Batch action. However, to answer your question, the slices in my layout evenly divide it into 8 images. Can the script, that you talk about, be run on more PSDs (as Batch)?

Kukurykus
Brainiac
December 12, 2021