How do I fully subtract one image from another?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
barry

How do I fully subtract one image from another?

Post by barry »

I'm new to ImageMagick and these forums and so am sorry to have signed up just to get some help! I hope I can do my bit by helping someone else here once I've got a bit more experience under my belt. Here's my challenge...

I have a whole lot (thousands) of 'original' images which look something like this:

Image

Each image has a unique filename.

I have an equivilent set of 'toremove' images which contain data which I need to remove from the original images. The one which matches the example above looks like this:

Image

all of these images are blue and so don't match the colours in the original. I need to perform the operation

destination.png = original.png - toremove.png

hence my destination file for the above should look like this:

Image

BUT... I'm performing this operation:

Code: Select all

composite "Original.png" "ToRemove.png" -compose dst_out "Destination.png"
And my destination file looks like this:

Image

So I'm not quite there - look carefully any you will see the 'ghost' of the removed data where some of the pixels remain. I also see that my image appears to have gained a white background and I'm not sure why that is happening. Can someone give me a command line instruction which will give me the desired result?

Thanks!

Barry.
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: How do I fully subtract one image from another?

Post by GreenKoopa »

barry wrote: I'm performing this operation:

Code: Select all

composite "Original.png" "ToRemove.png" -compose dst_out "Destination.png"
Wouldn't it be: ?

Code: Select all

composite "ToRemove.png" "Original.png" -compose dst_out "Destination.png"
And I don't produce that problem with the transparent background turning white. Are you using an older version?

As for the ghosting, that can be a problem with semi-transparency. Several solutions come to mind including:
Make all the semi-transparent pixels in ToRemove.png fully opaque
-- OR --
As I understand, ToRemove.png is only used for its alpha channel (the color is always blue). Combine the two alpha channels and then copy the result into Original.png
barry

Re: How do I fully subtract one image from another?

Post by barry »

Thanks for your reply GreenKoppa.

I tried your revised command line and it appears to do the same thing as the one I was using, is there a reason why your revision is preferable? Is there something there which I can't see?

The version I have is:

Version: ImageMagick 6.6.5-7 2010-11-06 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2010 ImageMagick Studio LLC
Features: OpenMP

but, I think we can ignore the white background issue - my local copy seems fine (transparent) and so I don't know why its appearing with a white background in the message I posted above.
As for the ghosting, that can be a problem with semi-transparency. Several solutions come to mind including:
Make all the semi-transparent pixels in ToRemove.png fully opaque
-- OR --
As I understand, ToRemove.png is only used for its alpha channel (the color is always blue). Combine the two alpha channels and then copy the result into Original.png
This is the key issue. Could you help me by providing command lines which would acheive these suggestions? I've been trying to work this out but am not doing well.

Thanks.
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: How do I fully subtract one image from another?

Post by GreenKoopa »

Nothing hidden; what you see is what you get. I don't know why the input file order doesn't matter for you, but no problem no worries I guess.

The first option is very simple:

Code: Select all

convert Original.png ( ToRemove.png -channel A -threshold 99% ) -compose dst_out -composite Destination.png
Is this sufficient for your needs?
barry

Re: How do I fully subtract one image from another?

Post by barry »

Thanks but I regret not :( If I do this I see this new destination.png:

Image
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: How do I fully subtract one image from another?

Post by GreenKoopa »

Well, this is more annoying. I am using version 6.6.5-8 2010-11-10 Q16 on Windows 7. I have been an ImageMagick user for only 2 weeks and so my help is worth what it is, but getting different results is unexpected and unhelpful. What if you set the threshold value to 1, 254, or 65530? Also note that (, ), and % may need to be escaped on particular platforms.
barry

Re: How do I fully subtract one image from another?

Post by barry »

Yes! you gave me the clue which solved it. I am running it from a Windows .BAT file, which of course means that % signs have special meaning. I changed my .BAT file to say '-threshold 99%%' (note the double %'s) and it works.

I really appreciate you help GreenKoopa - a big thank you :D
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: How do I fully subtract one image from another?

Post by GreenKoopa »

One of our mysteries solved then. I hope you are on the path to what you needed. Providing example images definitely helped in understand your question, so thank you for that.
Post Reply