Get difference between two images as transparent image in GIMP

I have two images A and B which are similar in many regions.

I want to create an image C so that

B = C on top of A

where C should be as transparent as possible. on top of is the normal layer superposition.

So C is some kind of difference between A and B. If A and B are identical, then C is totally transparent. If A and B are very different, then their C would be nearly identical to B.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You could use the “difference” layer mode to create a layer mask, and apply it to B like this:

  1. Open both images as layers, A on the bottom, B on top
  2. On the layer’s dialog, mark B’s layer mode as “difference”. You now
    should see the image mostly black is the images are alike, with the
    colored areas marking the areas that are different. These colors,
    however, won’t match B’s colors as you want
  3. Edit->Copy visible, to get a copy of the calculated effect on B
  4. Duplicate Layer B, change it’s mode back to “Normal” (let’s call this layer C)
  5. On the Layers menu, add a layer mask to C (Layer->Mask->Add Layer Mask).
  6. With the mask selected, paste the contents copied in step 3 (edit->paste).
  7. Anchor the pasted contents on the layer’s mask (layer->Anchor layer)

There you are – make Layers A and B invisible, and you should be very close to what you want. Further operations on the layer mask (I recommend using Colors->curve) can adjust the opacity/transparency of the areas on layer C.

update I recently answered a similar question to this on Stackoverflow, where the OP wanted to perform the task programatically (Using Python + Python-imaging-library) – I’ve posted the full program on that question and it might be of help for anyone interested in this answer.

Method 2

There is a simple method to do it:

Open the images as two layers, run Filters – Animation – Optimize (Difference), you should get the same resulting image that is now made of the bottom layer and a diff layer on top of it.

The things get complicated only if you have partial transparency, because the filter removes it.
Here is a workaround I’ve found for this case:

  • Remove Alpha Channel on both layers (the action is in the context menu)

The images will receive white background, but don’t worry, we’ll undo it later.

  • Filters – Animation – Optimize (Difference)

Now we have the difference layer with white background in places of partial transparency, but we need those pixels to be transparent, so we’ll take the shape of the diff and copy the image of this shape from the original top layer. Ensure the newly generated diff layer is selected and continue:

  • Layer – Layer to Image Size
  • Edit – Copy (Ctrl+C)
  • Return to the original image
  • Undo, Undo to return transparency to our layers
  • Edit – Paste as – New Layer
  • Alpha to Selection (in the context menu) on the pasted diff layer
  • Delete the pasted diff layer
  • Ensure that the top layer is selected and Copy (Ctrl+C) from it
  • Now you have the diff, just Paste (Ctrl+V) it and create a new layer from it.

Of course, no magic happens, and the partially transparent top layer won’t alter colors or reduce transparency of the bottom layer, so it’s only for cases of extension, where the altered pixels of the top layer cover the bottom layer.

P.S. jsbueno’s method didn’t work out for me.

Method 3

GIMP has a brush mode named “Color erasing”. Applying this brush, colored with color X, to definite pixel P1 creates a pixel P2 with lowered alpha channel, the way that blending P2 with pixel with color X in normal mode results pixel P1. Same effect, applied to selected region or whole image can be achieved using “Color to Alpha” action.

Brush mode could also be applied to “Clone tool”. In this case it is possible to take color from pixels of background image A and apply it to corresponding pixels of result image B. Image that has left is desired image C.

In details creation of image C is the following:

  1. Put images A and B as layers (A below B).
  2. Duplicate image B and call it C.
    Put it between A and B.
    Hide image B.
  3. Choose “Clone Tool”.
    On “Tool options” pane set following options:
    Mode: “Erase color”;
    Brush: use sharp round brush (“Hardness 100”)
    Size: make brush larger to easily paint all the image at once.
  4. Select A layer and CTRL + on top left pixel to
    select a place from which to clone and remember its position
    (for 100% zoom it will be (1,1), for 50% it will be (2,2) etc).
  5. Now select layer C and start painting from the top left and cover
    all the image without lifting the brush. It may seem that image
    does not change. But if you hide layer A, you’d see that layer C
    now contains desired image.
  6. Check result:
    Show layer A and layer C.
    Toggle layer B visibility to be sure that images look the same.
    (You could also choose Mode “Difference” for layer B, in such
    case you should get absolutely black image.)

Note: This method works as described if background image is opaque.
On transparent parts of background image, blending C on the top of A
looks different from image B.

Method 4

I know this is an old question, but just in case anyone else needs a relatively simple answer.

Presuming that you have the following:

  • An image (A)
  • Another image (B) that consists of the first image (A) overlaid with a third image (C) that you do not have a separate file for
  • A need to construct a clean copy of image C (perhaps to turn the overlay on and off)

In Gimp v2.10.18 do the following:

  • File > Open… the combined image B (this should appear as a single layer)
  • File > Open as layers… the image A (without the image C overlay)
  • In the Layers panel, select the layer containing the second image
  • Layer > Stack > Layer to Top
  • In the Layers panel, right click on top layer > Edit Layer Attributes…
  • Set mode to “Color erase” and click OK

Since we don’t have a definitive record of what image C had in each pixel before being combined with image A we cannot determine which pixels had the same content in both image A and C (if any). So, what this technique leaves us is a clear version of image C with slight gaps where pixel data from the two images would be equal (again, if any).

Using this technique I was able to create pristine recreations of text overlays given an image and the same image with overlaid tags for features in the image.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x