Compose Mask and Transparency using magick.. -composite

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?".
svengineer99
Posts: 24
Joined: 2016-01-09T10:38:11-07:00
Authentication code: 1151

Compose Mask and Transparency using magick.. -composite

Post by svengineer99 »

Running Windows ImageMagick 7.0.1-2 Q16 (64-bit) (2016-05-08)

I'm having trouble with alpha masking. Unfortunately the documentation at http://imagemagick.org/Usage/compose/#mask is obsolete (using composite command). Trying to use magick.. -composite I'm not getting the expected result:

Code: Select all

  magick -size 60x60 xc:none -fill red  -draw "circle 35,21 35,3"  m_src.png
  magick -size 60x60 xc:none -fill blue -draw "circle 21,39 24,57" m_bgnd.png
  magick -size 60x60 xc:   -draw "polygon 0,59 59,0, 0,0"          m_mask.png

  magick m_bgnd.png m_src.png -composite                 m_over.png
  magick m_bgnd.png m_src.png   m_mask.png -composite     m_over_masked.png
Produces an image with white portion of the mask turned black in what should be transparent background:
Image

I guess I must have some problem with the magick .. -composite masking syntax?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Compose Mask and Transparency using magick.. -composite

Post by snibgo »

This looks like a v7 bug.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compose Mask and Transparency using magick.. -composite

Post by fmw42 »

It does not work in IM 6 either. I do not think you can use a mask image directly on two transparent images. You will need to multiply the mask times the alpha channel of the two images and then flatten them over a transparent background.

At 6.9.0.0 for example, the output is transparent, but one sees the full blue circle and half the red circle. So even that is not correct.

Both may be a bug in both cases, but I am not sure how IM handles the fact that there are two transparent image and transparency controlled by the mask image.

This will work:

Code: Select all

magick  m_bgnd.png m_src.png \( m_mask.png -write mpr:mask +delete \) \
\( -clone 0 -alpha extract mpr:mask  -compose multiply -composite \
-clone 0 +swap -alpha off -compose over -compose copy_opacity -composite +write show: \) \
\( -clone 1 -alpha extract \( mpr:mask -negate \) -compose multiply -composite \
-clone 1 +swap -alpha off -compose over -compose copy_opacity -composite +write show: \) \
-delete 0,1 -background none -compose over -flatten \
m_over_masked.png
svengineer99
Posts: 24
Joined: 2016-01-09T10:38:11-07:00
Authentication code: 1151

Re: Compose Mask and Transparency using magick.. -composite

Post by svengineer99 »

Thanks for confirming it. Could you provide an example how to 'multiply the mask times the alpha channel of the two images and then flatten them over a transparent background' for the case above? My experience with getting IM syntax correct on my own is not so good.

Edit -sorry I see you included an example already. IN that case - thank you! I will try it out now..
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Compose Mask and Transparency using magick.. -composite

Post by snibgo »

fmw42 wrote:At 6.9.0.0 for example, the output is transparent, but one sees the full blue circle and half the red circle. So even that is not correct.
v6.9.2-5 gives that result, which is the correct result.

The mask is black at top-left and white at bottom right. So the effect masked "over" composite should be that we get the "dst" image top-left, and "src over dst" bottom-right.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compose Mask and Transparency using magick.. -composite

Post by fmw42 »

snibgo, I am not getting what you imply. I get a full blue circle and a half red circle. It should be two half circles, should it not?

Code: Select all

im6925 convert -size 60x60 xc:none -fill red  -draw "circle 35,21 35,3"  m_src.png
im6925 convert -size 60x60 xc:none -fill blue -draw "circle 21,39 24,57" m_bgnd.png
im6925 convert -size 60x60 xc:   -draw "polygon 0,59 59,0, 0,0"          m_mask.png

im6925 convert m_bgnd.png m_src.png m_mask.png -composite m_over_masked6925.png
Image

Is this what you see?

For reference:

IM6944

Code: Select all

convert -size 60x60 xc:none -fill red  -draw "circle 35,21 35,3"  m_src.png
convert -size 60x60 xc:none -fill blue -draw "circle 21,39 24,57" m_bgnd.png
convert -size 60x60 xc:   -draw "polygon 0,59 59,0, 0,0"          m_mask.png

convert m_bgnd.png m_src.png m_mask.png -compose over -composite m_over_masked_6944.png
Image


IM7016

Code: Select all

im7 magick -size 60x60 xc:none -fill red  -draw "circle 35,21 35,3"  m_src.png
im7 magick -size 60x60 xc:none -fill blue -draw "circle 21,39 24,57" m_bgnd.png
im7 magick -size 60x60 xc:   -draw "polygon 0,59 59,0, 0,0"          m_mask.png

im7 magick m_bgnd.png m_src.png  m_mask.png -composite     m_over_masked_7016.png
Image



So to get two half circle, my code from above was:

Code: Select all

convert m_bgnd.png m_src.png \( m_mask.png -write mpr:mask +delete \) \
\( -clone 0 -alpha extract mpr:mask  -compose multiply -composite \
-clone 0 +swap -alpha off -compose over -compose copy_opacity -composite +write show: \) \
\( -clone 1 -alpha extract \( mpr:mask -negate \) -compose multiply -composite \
-clone 1 +swap -alpha off -compose over -compose copy_opacity -composite +write show: \) \
-delete 0,1 -background none -compose over -flatten \
m_over_masked.png
Image


So clearly there is a bug, at minimum, in the IM 7 processing, since it does not even match that of IM 6.
svengineer99
Posts: 24
Joined: 2016-01-09T10:38:11-07:00
Authentication code: 1151

Re: Compose Mask and Transparency using magick.. -composite

Post by svengineer99 »

@fmw42 - Thanks for the example. It works for me as you described. However, what I was really after was the half red circle over the full blue circle (masking one alpha image placed over another alpha image). Could I trouble you for another example how to do that? Actually just masking the half red circle (just masking an alpha image similar to http://stackoverflow.com/questions/1364 ... magemagick) would be enough for me, if there's a simpler implementation.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Compose Mask and Transparency using magick.. -composite

Post by snibgo »

Fred: Half a red circle, with a complete blue circle, yes, that's the image I get, and it's the correct result.

Where the mask is black, the result should be m_bgnd.png. It is.

Where the mask is white, the result should be m_src OVER m_bgnd. It is.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compose Mask and Transparency using magick.. -composite

Post by fmw42 »

I was interpreting the mask composite operation so as to take the blue transparent circle image where it was black and red transparent circle image where it was white. That is top left of first image and bottom right of second image. Thus two half circles.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compose Mask and Transparency using magick.. -composite

Post by fmw42 »

svengineer99 wrote:@fmw42 - Thanks for the example. It works for me as you described. However, what I was really after was the half red circle over the full blue circle (masking one alpha image placed over another alpha image). Could I trouble you for another example how to do that? Actually just masking the half red circle (just masking an alpha image similar to http://stackoverflow.com/questions/1364 ... magemagick) would be enough for me, if there's a simpler implementation.

Code: Select all

convert swCkD.png gHRFF.png \
\( -clone 0 -alpha extract \) \
\( -clone 1 -clone 0 -compose multiply -composite \) \
-delete 1,2 \
-alpha off -compose copy_opacity -composite \
result.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Compose Mask and Transparency using magick.. -composite

Post by snibgo »

fmw42 wrote:I was interpreting the mask composite operation so as to take the blue transparent circle image where it was black and red transparent circle image where it was white. That is top left of first image and bottom right of second image. Thus two half circles.
That's not what a masked composition should do.

Where the mask is white, the result should be the same as the un-masked composite.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compose Mask and Transparency using magick.. -composite

Post by fmw42 »

Where the mask is white, the result should be the same as the un-masked composite.
That has not been how I have interpretted masked composite with -compose overlay. When the two images are not transparent, you get the upper left for where the mask is black and the bottom right where the mask is white. So you should see two half circles.

Code: Select all

convert -size 60x60 xc:gray -fill red  -draw "circle 35,21 35,3"  m_src2.png
convert -size 60x60 xc:gray -fill blue -draw "circle 21,39 24,57" m_bgnd2.png
convert -size 60x60 xc:   -draw "polygon 0,59 59,0, 0,0"          m_mask2.png
convert m_bgnd.png m_src.png m_mask.png -compose over -composite m_over_masked2.png
Image

So I assumed that the same would happen even if the images were transparent.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Compose Mask and Transparency using magick.. -composite

Post by snibgo »

Your inputs are opaque, so the result of "src OVER dst" is src.

But when src has any transparency, the result of "src OVER dst" is neither src nor dst.
snibgo's IM pages: im.snibgo.com
svengineer99
Posts: 24
Joined: 2016-01-09T10:38:11-07:00
Authentication code: 1151

Re: Compose Mask and Transparency using magick.. -composite

Post by svengineer99 »

Code: Select all

convert swCkD.png gHRFF.png \
\( -clone 0 -alpha extract \) \
\( -clone 1 -clone 0 -compose multiply -composite \) \
-delete 1,2 \
-alpha off -compose copy_opacity -composite \
result.png
[/quote]

Sorry, I'm not sure which should be the mask and which should be the source, but I'm not getting a good result with either:

Code: Select all

convert m_mask.png m_src.png ( -clone 0 -alpha extract ) ( -clone 1 -clone 0 -compose multiply -composite ) -delete 1,2 -alpha off -compose copy_opacity -composite result.png
Image

Code: Select all

convert m_src.png m_mask.png ( -clone 0 -alpha extract ) ( -clone 1 -clone 0 -compose multiply -composite ) -delete 1,2 -alpha off -compose copy_opacity -composite result.png
Image

Sorry, I have no idea why my images aren't embedding correctly..
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compose Mask and Transparency using magick.. -composite

Post by fmw42 »

OK. You know the code better than I. I had hoped that functionally masking would be the same whether it was transparent or not. But that was why I was concerned at the beginning of my first reply regarding masking of two transparent images, leading me to create my command line that properly produced two half circles.
Post Reply