6 Replies Latest reply on Dec 16, 2011 7:54 PM by hujabub

    Comparing layers for equality


      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.

        • 1. Re: Comparing layers for equality

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

          • 2. Re: Comparing layers for equality
            xbytor2 Level 4

            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.

            • 3. Re: Comparing layers for equality
              Muppet Mark Level 5

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

              • 4. Re: Comparing layers for equality
                xbytor2 Level 4

                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.

                • 5. Re: Comparing layers for equality
                  Muppet Mark Level 5

                  Ah… no Voodoo then this time… I was thinking a similar thing only… I think I would have probably aligned all layers in the document centrally ( except b/g ) then worked down the stack… removing untill uniques are left… I couldn't think of another method other than 'difference' to check thou…

                  • 6. Re: Comparing layers for equality
                    hujabub Level 1

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


                    Cheers ^^