Subtract opaque region of 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
mortoray
Posts: 17
Joined: 2014-02-12T21:36:17-07:00
Authentication code: 6789

Subtract opaque region of one image from another

Post by mortoray »

I want to subtract two images from each other but cannot deterine what command-line is appropriate. I want the opaque regions of a mask image to be removed from another image.

Image minus Image equals Image

I have this command-line which works with the above:

Code: Select all

convert -channel Alpha -compose subtract -composite circle.png square.png -write mpr:orig +delete \
        -channel RGB -compose add -composite mpr:orig square.png output.png
However, the moment the mask is smaller, like this Image, the target image is also smaller. I want to keep the source images size. I would then use some kind of geometry command to offset the mask into the larger one.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Subtract opaque region of one image from another

Post by snibgo »

I would copy the opacity, then negate it:

Code: Select all

convert square.png smallcircle.png -gravity center -compose CopyOpacity -composite -channel A -negate a.png
snibgo's IM pages: im.snibgo.com
mortoray
Posts: 17
Joined: 2014-02-12T21:36:17-07:00
Authentication code: 6789

Re: Subtract opaque region of one image from another

Post by mortoray »

That appears to do what I want. I'm not sure I fully understand how it works. The opacity from circle_small is copied into the merged image, which retains the colors and size of square. The resulting image has it's alpha channel inverted. Is that correct?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Subtract opaque region of one image from another

Post by snibgo »

What "merged" image?

The square image is entirely opaque. Each circle image has an opaque circle against a transparent background. I copy the opacity from the circle image to the square image. Now the square image is opaque in the centre with transparent corners (just like the circle image), and this is the opposite of what you want. So I negate the alpha channel.

"-gravity center" is needed when the circle image is smaller than the square image.
snibgo's IM pages: im.snibgo.com
mortoray
Posts: 17
Joined: 2014-02-12T21:36:17-07:00
Authentication code: 6789

Re: Subtract opaque region of one image from another

Post by mortoray »

I understand now clearly. Thank you.
Post Reply