21

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.

23

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.

  • On the layer's dialog, mark B's layer mode as "difference". Sorry, what? I'm looking at the Layers tool window, and if I right click on layer B, I don't see anything that looks like a layer mode. Closest I've found is Add Layer Mask, but I don't think that is what you are referring to. Using v2.8.22. – Adrian Feb 7 '18 at 21:09
  • with the layer selected, the layer mode can be chosen on a drop-down on the top of the layers dialog, not from the right-click menu. – jsbueno Feb 7 '18 at 22:19
  • I'm having a bit of confusion or trouble with step 6 - it seems to be pasting the contents into a new layer, even with "C mask" selected, making a fourth layer named "Floating Selection (Pasted Layer)" which is offset from the original position - is that the way it should look? – Code Jockey Aug 30 '18 at 12:59
  • It is a "floating selection" until you "anchor" it to its destination (Ctrl-H). Make sure that the mask is active when you paste (Ctrl-V) otherwise the anchoring may go elsewhere. – xenoid Aug 30 '18 at 23:07
5

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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.