Page 1 of 1

Subtract opaque region of one image from another

Posted: 2014-02-12T22:21:43-07:00
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.

Re: Subtract opaque region of one image from another

Posted: 2014-02-12T23:10:17-07:00
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

Re: Subtract opaque region of one image from another

Posted: 2014-02-13T00:14:08-07:00
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?

Re: Subtract opaque region of one image from another

Posted: 2014-02-13T01:00:30-07:00
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.

Re: Subtract opaque region of one image from another

Posted: 2014-02-13T01:27:28-07:00
by mortoray
I understand now clearly. Thank you.