Skip to main content
Participant
December 15, 2011
Question

Comparing layers for equality [2011]

  • December 15, 2011
  • 3 replies
  • 3294 views

Is it possible to compare two different layers to see if they are in fact the same image?

 

I'm actually working on a game at the moment. The game map was created by using game sprites created by other people. For example, I found 5 different pictures of trees, and copy pasted them into photoshop multiple times to create a forest. I have 100 trees in my photoshop document, but in reality, they are all made up of only 5 images. I want to save out each of these trees into a format usable by my game engine. I want to save each layer as an image, but avoid saving duplicates. Thus the need to compare layers for equality.

 

Is it possible to do this within a photoshop script? Thanks for any help you can provide.

3 replies

hujabubAuthor
Participant
December 17, 2011

Thanks for the great suggestions. I'll give them a shot over the weekend and let you know how it goes.

Cheers ^^

Known Participant
February 25, 2025

Hi , Did any one work on this script .

Stephen Marsh
Community Expert
Community Expert
February 25, 2025
quote

Hi , Did any one work on this script .


By @Chandrakanthi26574696ugut

 

This topic thread is around 14 years old, are your criteria the same? In it's simplest form, you only need an action, not a script. What would such a script offer? Is this only for two layers to compare in a single document? Can you screenshot the layers panel and provide any further info?

Inspiring
December 16, 2011

Make just the two layers visible. Set the blend mode on the top layer to Difference. Then  try this:

app.activeDocument.histogram.slice(1).toString().replace(/,0/g, "") == "0"

This will evaluate to true if the layers are identical and false if there are any differences.

Inspiring
December 16, 2011

X, that is how I would check using the UI as a visual check, I think it could work… but wouldn't this only work in the instance of them being on top of each other? ( pixel for pixel ) I read this that a forest may be a couple of tress as such but my guess is they are placed at random locations… or did I just overlook some of your clever trickery/voodoo…?

Inspiring
December 16, 2011

but wouldn't this only work in the instance of them being on top of each other?

That bit of code presumes:

  1. The layers are in the same document
  2. The layers are the only two layers visible
  3. The content of the layers is aligned.
  4. The layers have simple content (no layer masks, vector masks, layer styles)

There may be other assumptions that I'm making, but this is all that I can think of.

For the trees/forest problem, I would iterate though the tree-layers.

Create a new document that is as large as the largest tree-layer in the forest.

Hide the background layer.

Copy the first layer to the new document.

Copy the second layer to the new doc

Do the layer comparison code from above my previous post

If the layers are the same, delete the new one.

Go to the next layer in the forest document.

Copy and compare the new layer against each layer in the new document.

Delete it if it's a dupe.

When you're done, the new document should have only the unique trees.

Inspiring
December 16, 2011

hm, what i'm thinking of does not work 100%, but no other idea comes to my mind.

Define 5 different variables as empty array

Now go through all your layers, apply the average filter on each and with the color sampler get the r, g and b values.

If the values do not exist in neither of those 5 variables, save them as one of the variables and save the image (e.g. by copying the layer to another document)

Should work in estimated 95% of the cases ;-)