Combinations and Permutations of Photoshop Layers?

This one is a big one

I have a Photoshop document that has 8 Layers in it. I need to create a way to use those 8 layers and create certain combinations depending on the rules below…


{1,2,3} {1,2,4} {1,2,5} {1,2,6} {1,2,7} {1,2,8} {1,3,4} {1,3,5} {1,3,6} {1,3,7} {1,3,8} {1,4,5} {1,4,6} {1,4,7} {1,4,8} {1,5,6} {1,5,7} {1,5,8} {1,6,7} {1,6,8} {1,7,8} {2,3,4} {2,3,5} {2,3,6} {2,3,7} {2,3,8} {2,4,5} {2,4,6} {2,4,7} {2,4,8} {2,5,6} {2,5,7} {2,5,8} {2,6,7} {2,6,8} {2,7,8} {3,4,5} {3,4,6} {3,4,7} {3,4,8} {3,5,6} {3,5,7} {3,5,8} {3,6,7} {3,6,8} {3,7,8} {4,5,6} {4,5,7} {4,5,8} {4,6,7} {4,6,8} {4,7,8} {5,6,7} {5,6,8} {5,7,8} {6,7,8}

Using the above combination, each combination needs to be saved out as an image. SO if you take one of the above combinations for instance, {3,6,8}. Photoshop should use layers 3,6,8 to create a new image or layer comprising of those three segments.

Not sure how to start this. So far I have worked out all possible combinations using this great site:


I had no idea you could script with JavaScript in Photoshop prior to reading @horatio’s answer, so I gave it a go.

If you’re new to scripting (first do some tutorials, then) I recommend stealing as much as you can from one of the scripts that ships with Photoshop, I made use of Layer Comps To Files.jsx (CS6) so I didn’t have to look up how to save files myself.

var a = 1; // number base layers
var p = 8; // number of layers used for permutations

var t = app.activeDocument.artLayers.length - 1; 

function hideAll() {
    for (var i = a; i < a+p; i++) {
        app.activeDocument.artLayers[t-i].visible = false;

for (var x = a; x < a+p; x++) {
    for (var y = x+1; y < a+p; y++) {
        for (var z = y+1; z < a+p; z++) {
            app.activeDocument.artLayers[t-x].visible = true;
            app.activeDocument.artLayers[t-y].visible = true;
            app.activeDocument.artLayers[t-z].visible = true;
            saveFile( app.activeDocument, x+'-'+y+'-'+z, exportInfo);

saveFile(...) I stole from the original script, though I changed it to immediately save as .jpg to avoid being prompted.
My use of the t variable is fairly confusing, I had assumed the 0th layer would be the base layer but it’s actually the top layer and t compensates for that.

I tried it out with a 9 layer image, 1 base layer and then 8 smilies. The result is 56 permutations in approximately 10 seconds:


I tried to think of a way to do this iteratively with actions, but with 8 layers you have to record the manual creation of almost half the permutations (21 of 56 total), and it involves deleting duplicates at the end, for this reason I don’t feel it is a particularly viable option.

If the order of the layers was important, this would work pretty well, you’d record 1/8th of the work, and no duplicates would be produced.

You’d record yourself creating the first 21 permutations show below, saving a copy at each stage, then at the final step, move the last layer into the first position (i.e. so layer 8 becomes layer 1). Save the action, then run it several more times and it’ll save all permutations.

For 8 layers, here are the permutations produced:


In the last row the #s indicate new permutations, everything else is a duplicate. So you can see each run of the action has diminishing returns and after 5 actions you have all possible permuations.

So if you run it 5 times you get 126 results (56 of which are useful).

It’d be a better idea to repeat the action 3 times, delete the last 9 images produced, reducing the duplicates to 17 and then produce the remaining 4 permutations manually.

Source : Link , Question Author : user3323371 , Answer Author : OGHaza

Leave a Comment