I have two images
B which are similar in many regions.
I want to create an image
C so that
B = C on top of A
Cshould be as transparent as possible.
on top ofis the normal layer superposition.
C is some kind of difference between
B are identical, then
C is totally transparent. If
B are very different, then their
C would be nearly identical to
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.
You could use the “difference” layer mode to create a layer mask, and apply it to B like this:
- Open both images as layers, A on the bottom, B on top
- 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
- Edit->Copy visible, to get a copy of the calculated effect on B
- Duplicate Layer B, change it’s mode back to “Normal” (let’s call this layer C)
- On the Layers menu, add a layer mask to C (Layer->Mask->Add Layer Mask).
- With the mask selected, paste the contents copied in step 3 (edit->paste).
- 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.
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.
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:
- Put images A and B as layers (A below B).
- Duplicate image B and call it C.
Put it between A and B.
Hide image B.
- 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.
- 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).
- 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.
- 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.
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.