DavidNyan10
DavidNyan10

Reputation: 199

How to generate all possible layer-visibility combinations of a Photoshop file

I have a .psd file with around 8 layers. But first, for simplicity, let's suppose a file with 4 layers:

Layer 0
Layer 1
Layer 2
Layer 3

This would give us 2^4 possible combinations, which is 16 images as output as follow:

None visible
Only 0 is visible
Only 1 is visible
Only 2 is visible
Only 3 is visible
0 and 1 are visible
0 and 2 are visible
0 and 3
1 and 2
1 and 3
2 and 3
0, 1, 2
0 1 3
0 2 3
1 2 3
All are visible

I could do this by hand but for 2^8 images, this would take a lot of time, and would be confusing like "Hmm, did I do this combination already?". Because for each combination, I'd need to export each image as an image file (preferably png), which would take a lot of time.

So, is there a way to programmatically achieve this? Maybe a script that could read the PSD file for layers and hide them or make the layers visible in a very large for loop where it will export as PNG for each combination.

And for generating the combination, I was thinking of binary, so for example, the 4 layers could be

0000
0001
0010
0011
...etc.

where each bit represents a layer, and this would be much faster for the 8 layers.

Upvotes: 0

Views: 113

Answers (1)

Ghoul Fool
Ghoul Fool

Reputation: 6967

Yes, it's possible with Photoshop script. There are a couple of things you'll have to work out, but the main loop is basically this:

displayDialogs = DialogModes.NO; // OFF 

// call the source document
var srcDoc = app.activeDocument;
var numOfLayers = srcDoc.layers.length;



// number of permutations (decimal)
var permutations = Math.pow(2,numOfLayers);

var lenny = permutations.toString(2).length -1;


for (var i = 0; i < permutations; i++)
{
  var bin = zfill(i.toString(2), lenny, "0");

  // switch the appropriate layers on
  for (var j = 0; j < bin.length; j++)
  {
    if (bin[j] == "1") srcDoc.layers[j].visible = true;
    else srcDoc.layers[j].visible = false;
  }

  var suffix = "_" + zfill(i, 2, "0");

  // duplicate it
  duplicate_document(suffix);

  var f = fpath + "\\" + suffix + ".png";

  // save it as png

  //close without saving
  app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

  // switch back to the original source document
  app.activeDocument = srcDoc;

  switch_all_layers_off(numOfLayers);
}

Note that switching layers off and on with a large number of layers in a Photoshop document is notoriously slow. Hopefully 16 layers won't be too painful.

Upvotes: 0

Related Questions