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

Example:

{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:

http://www.mathsisfun.com/combinatorics/combinations-permutations-calculator.html

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++) {
hideAll();
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.

• – Love the faces ;) May 7, 2014 at 20:16
• @Rsiel thanks ;) May 7, 2014 at 20:41
• Wow :o I am not that versed with using photoshop scripts so i am using jdigioia method below by creating an action for each combination as i am pressed for time. If you do feel like sharing the above script it will be beneficial. But thanks for the response. Love the faces as well. May 8, 2014 at 12:58
• @user3323371 No probs, I only went into so much depth with the second part because I was hoping to have a eureka moment where I discovered some super quick & easy method. Alas, I got to the end and had a complicated mess ;) Script is at home right now, will link it up in a comment later. May 8, 2014 at 13:07
• Fantastic :D You do not understand how grateful i am. Sincerely. Thank You. May 13, 2014 at 21:31

The javascript command to hide/show a layer is:

activeDocument.artLayers[i].visible = true;

The rest is academic: set up an array (or just use your combinatorial algorithm), iterate, toggle visibility, "save as copy".

So my first thought is to save the 50+ combinations out by setting all of the other layers to hidden except the layers in the current 3 number combo and just save for web and devices as an image, but I would think you want to make this repeatable.

If you want to do this in the future with different photos you could create a custom Photoshop Action for each combo if that is your end goal to be able to do this again and again with different photos in the same 8 layers. The link below walks you through how to create and save an Action and below is my thought on how to save each combination of 3 as a custom action so you can replace the 8 photos at will and be able to use all of the combinations in the future.

http://www.hongkiat.com/blog/photoshop-tutorial-1-click-photo-editing-with-action/

See link on the set up, but once you are ready you could:

• 'Hide' all layers
• Hit record on your action
• 'Show' a select 3 images of the 50+ combos
• Save for web and devices - Do that for each combination or all in the same action if you want (!)
• End Recording

Either way, you still have to either manually set it up and save out the photos saving each as an action OR just do that once and get your image sets from the same 8 photos.

Hope this is at least a start! Sounds like a cool idea for sure.

Joe

Per jdigioia's answer, create your Action, showing and hiding the layers, but after each instance, Create a Layer Comp.

When you run the Action, you should have a Layer Comp for each instance.

Finally, use File / Scripts / Layer Comps to Files to export your images.

It would probably be best to do them in separate Actions. ie. one Action for all possibilities with Layer 1,
Then Duplicate that Action and add/move/duplicate or delete steps to make all possibilities with Layer 2, etc.