• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Script for splitting multiple PSDs to defined slices

Community Beginner ,
Dec 12, 2021 Dec 12, 2021

Copy link to clipboard

Copied

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.

 

TOPICS
Actions and scripting

Views

1.5K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
LEGEND ,
Dec 12, 2021 Dec 12, 2021

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 12, 2021 Dec 12, 2021

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 12, 2021 Dec 12, 2021

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 12, 2021 Dec 12, 2021

Copy link to clipboard

Copied

Ah, of course, life is never simple.

 

Kukurykus posted a great link. Can I also presume that you don't know how to script Photoshop yourself?

 

EDIT: I presume that the slices are created in the parent PSD before you create the variable driven PSD child files? So when you open each file generated by the variable output it contains the slice info? If so, using the Export > Save for Web (Legacy) option can be recorded into an action for batching and it will automatically create output files from slices in say JPEG or PNG formats (do you really need PSD as output format from slices?).

 

https://helpx.adobe.com/photoshop/using/slicing-web-pages.html

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 12, 2021 Dec 12, 2021

Copy link to clipboard

Copied

Like I said, the situation is much more complex, there would be tens, maybe thousands child PSDs 🙂 Nevertheless, I have found one possible solution - record action for cropping the image and use it in Batch.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 12, 2021 Dec 12, 2021

Copy link to clipboard

Copied

Unfortunately your posts and objections make little sense to me.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 12, 2021 Dec 12, 2021

Copy link to clipboard

Copied

Why a little sense? I have one PSD consisting of 8 slices with maybe 140 localised texts. I imported them using Dataset. Therefore, when I export PSDs as Datasets, there are cca 140 PSDs (still not splitted into slices). The next step is to split PSDs into slices. When I find some solution for that, resulting PSDs/JPGs would count to 140*8=1120. I believe that you understand now, that I cannot do all the actions manually.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 12, 2021 Dec 12, 2021

Copy link to clipboard

Copied

quote

Why a little sense? I have one PSD consisting of 8 slices with maybe 140 localised texts. I imported them using Dataset. Therefore, when I export PSDs as Datasets, there are cca 140 PSDs (still not splitted into slices). The next step is to split PSDs into slices. When I find some solution for that, resulting PSDs/JPGs would count to 140*8=1120. I believe that you understand now, that I cannot do all the actions manually.


By @Tom114K

 

This is logical, whether slicing into 2 or 8 etc. When I suggested doing similar, you wrote:

 

"Like I said, the situation is much more complex, there would be tens, maybe thousands child PSDs 🙂 Nevertheless, I have found one possible solution - record action for cropping the image and use it in Batch."

 

Which is pretty much what I originally suggested before you stated that the situation was more complex than your original example.

 

So we're going around in circles.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 12, 2021 Dec 12, 2021

Copy link to clipboard

Copied

Yes, batch action for Save for Web would be the best solution, unfortunately Save for Web functionality does not work for me, when there are any slices (The export fails with message: "Could not write the file. The unknown error has occured").

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 12, 2021 Dec 12, 2021

Copy link to clipboard

Copied

quote

Yes, batch action for Save for Web would be the best solution, unfortunately Save for Web functionality does not work for me, when there are any slices (The export fails with message: "Could not write the file. The unknown error has occured").


By @Tom114K

 

Well that is not normal/expected behaviour and was previously unknown as it was not mentioned until now. 😐

 

What are the pixel dimensions of the PSD that you are saving into slices using save for web? Can you show a screenshot of the image with the slices visible so that the complexity of the slicing is apparent?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 13, 2021 Dec 13, 2021

Copy link to clipboard

Copied

The dimensions of the parent PSD are 8640x1920. However, I've found out that it could be caused by the fact that I am saving child PSDs into Dropbox - cloud services may be the reason. So I will test it without Dropbox, I hope it will work. Thank you very much for your help.

 

However, one more note for the Save for Web action - when I record such action, and then try to run it as a batch, the resulting child PSDs are only from the last action run (last parent PSD) - the names of saved PSDs are not changing, therefore the results are rewritten. In the Batch window, File Naming is working for Save As command, not Save for Web. Therefore, this solution might not work as we both expect.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 13, 2021 Dec 13, 2021

Copy link to clipboard

Copied

Ah, I believe that 8192 px is the size limit I'm afraid.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 13, 2021 Dec 13, 2021

Copy link to clipboard

Copied

Size limit for what? Could you please provide a link to this information?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 13, 2021 Dec 13, 2021

Copy link to clipboard

Copied

Oh I see now. You mean size limit for Save for Web.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 13, 2021 Dec 13, 2021

Copy link to clipboard

Copied

Yes, it was never designed for such sizes and does funky things if fed a larger size original:

 

S4W-8192px-Limit.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 13, 2021 Dec 13, 2021

Copy link to clipboard

Copied

OK, I will record an action for cropping the image and use it in Batch as previously mentioned. More work at first, but it would time in the future. Thank you.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 13, 2021 Dec 13, 2021

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 13, 2021 Dec 13, 2021

Copy link to clipboard

Copied

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)?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 13, 2021 Dec 13, 2021

Copy link to clipboard

Copied


@Tom114K wrote:

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)?


 

I can't see why not, but it will depend on the script – I'll need to test...

Is the slice layout 1 row with 8 columns, or is it 2 rows x 4 columns (or 4 x 2)?

 

EDIT: Yes, this can be batched, I'll make a new post!

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 13, 2021 Dec 13, 2021

Copy link to clipboard

Copied

quote

Oh I see now. You mean size limit for Save for Web.


By @Tom114K

 

 

One can pre-crop to reduce the image to <= 8192px, then use S4W to save out all of the predefined slices within the cropped area to separate files. So one could setup a batch to pre-crop the content, save out all the slices, then revert to before the crop, dupe the file (new name to avoid overwriting) and crop from the other side and save out all the slices. This way one avoids the pixel size limit and can still take advantage of using the slices to save separate files. This could all be put into a batch action.

 

Then all one would need to do is a batch rename using Adobe Bridge to normalise the filenames between the originals and the duped file.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 13, 2021 Dec 13, 2021

Copy link to clipboard

Copied

quote

However, one more note for the Save for Web action - when I record such action, and then try to run it as a batch, the resulting child PSDs are only from the last action run (last parent PSD) - the names of saved PSDs are not changing, therefore the results are rewritten. In the Batch window, File Naming is working for Save As command, not Save for Web. Therefore, this solution might not work as we both expect.


By @Tom114K

 

 

You can change where you save the file in the action, but don't change the name and it will only record the save path but not the name. Then when using in batch it will use each separate filename and save out the slices correctly without overwriting.

 

It does work as expected when done correctly, I did test this workflow before proposing it just to be sure that it all worked as I remembered.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 13, 2021 Dec 13, 2021

Copy link to clipboard

Copied

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-...

 

/* 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-...

 

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/1246703...

 

Instructions for saving/installing code:

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

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 13, 2021 Dec 13, 2021

Copy link to clipboard

Copied

Thank you very much, I will test that out.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Dec 14, 2021 Dec 14, 2021

Copy link to clipboard

Copied

LATEST
quote

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/1246703...


By @Stephen_A_Marsh

 

I liked this idea and later I rewrote the script for myself, just to save slices in psd. However, there is a small problem - it will not work in CS5, since the ability to get information about the slices of the document was added quite recently.

 

/**Divide my image to layers
 * https://community.adobe.com/t5/photoshop-ecosystem-discussions/divide-my-image-to-layers/m-p/12467520
 */
#target photoshop
var s2t = stringIDToTypeID,
    AR = ActionReference,
    AD = ActionDescriptor;

try {
    try {
        (r = new AR).putProperty(s2t('property'), p = s2t('layerID'));
        r.putEnumerated(s2t('layer'), s2t('ordinal'), s2t('targetEnum'));
        var id = executeActionGet(r).getInteger(p);
    }
    catch (e) { throw "No layer selected!\nOpen the document and select layer" }

    try {
        (r = new AR).putProperty(s2t('property'), p = s2t('slices'));
        r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
        var slices = executeActionGet(r).getObjectValue(p).getList(p);
    }
    catch (e) { throw "This version of photoshop does not have access to slices" }

    (r = new AR).putProperty(s2t('property'), p = s2t('resolution'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var res = executeActionGet(r).getDouble(p);

    (r = new AR).putProperty(s2t('property'), p = s2t('title'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var nm = executeActionGet(r).getString(p).replace(/\..+$/, '');

    try {
        (r = new AR).putProperty(s2t('property'), p = s2t('fileReference'));
        r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
        var pth = executeActionGet(r).getPath(p);
    }
    catch (e) { throw "File not saved!" }

    for (var i = 0; i < slices.count - 1; i++) {
        (r = new AR).putIdentifier(s2t('layer'), id);
        (d = new AD).putReference(s2t('target'), r);
        executeAction(s2t('select'), d, DialogModes.NO);

        (r = new AR).putProperty(s2t('channel'), s2t('selection'));
        (d = new AD).putReference(s2t('target'), r);
        d.putObject(s2t('to'), s2t('rectangle'),
            function (b, d) {
                for (var i = 0; i < b.count; i++)
                    d.putUnitDouble(k = (b.getKey(i)), s2t('pixelsUnit'), b.getInteger(k))
                return d;
            }(slices.getObjectValue(i).getObjectValue(s2t('bounds')), new AD)
        );
        executeAction(s2t('set'), d, DialogModes.NO);

        try {
            (d = new AD).putString(s2t("copyHint"), "pixels");
            executeAction(s2t("copyEvent"), d, DialogModes.NO);

            (d = new AD).putClass(s2t("mode"), s2t("RGBColorMode"));
            d.putUnitDouble(s2t("width"), s2t("distanceUnit"), 1 * 72 / res);
            d.putUnitDouble(s2t("height"), s2t("distanceUnit"), 1 * 72 / res);
            d.putUnitDouble(s2t("resolution"), s2t("densityUnit"), res);
            d.putEnumerated(s2t("fill"), s2t("fill"), s2t("white"));
            d.putInteger(s2t("depth"), 8);
            d.putString(s2t("profile"), "sRGB IEC61966-2.1");
            (d1 = new AD).putObject(s2t("new"), s2t("document"), d);
            executeAction(s2t("make"), d1, DialogModes.NO);

            (d = new AD).putEnumerated(s2t("antiAlias"), s2t("antiAliasType"), s2t("antiAliasNone"));
            d.putClass(s2t("as"), s2t("pixel"));
            executeAction(s2t("paste"), d, DialogModes.NO);

            executeAction(s2t("revealAll"), new AD, DialogModes.NO);
            executeAction(s2t("flattenImage"), undefined, DialogModes.NO);

            (d = new AD).putObject(s2t("as"), s2t("photoshop35Format"), new AD);
            d.putPath(s2t("in"), File(pth.path + '/' + nm + ' ' + ('0' + i).slice(-2) + '.psd'));
            d.putEnumerated(s2t("saveStage"), s2t("saveStageType"), s2t("saveBegin"));
            executeAction(s2t("save"), d, DialogModes.NO);

            executeAction(s2t("close"), new AD, DialogModes.NO);

            (r = new AR).putProperty(s2t('channel'), s2t('selection'));
            (d = new AD).putReference(s2t('null'), r);
            d.putEnumerated(s2t('to'), s2t('ordinal'), s2t('none'));
            executeAction(s2t('set'), d, DialogModes.NO);
        }
        catch (e) { throw e + "\nScript cannot create layer from empty space!\nMake sure that current layer contains pixels in all slices." }
    }
} catch (e) { alert(e) }

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines