Page 1 of 3
Compose Mask and Transparency using magick.. -composite
Posted: 2016-05-24T14:09:52-07:00
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:
I guess I must have some problem with the magick .. -composite masking syntax?
Re: Compose Mask and Transparency using magick.. -composite
Posted: 2016-05-24T14:42:37-07:00
by snibgo
This looks like a v7 bug.
Re: Compose Mask and Transparency using magick.. -composite
Posted: 2016-05-24T14:59:36-07:00
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
Re: Compose Mask and Transparency using magick.. -composite
Posted: 2016-05-24T15:16:20-07:00
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..
Re: Compose Mask and Transparency using magick.. -composite
Posted: 2016-05-24T15:18:30-07:00
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.
Re: Compose Mask and Transparency using magick.. -composite
Posted: 2016-05-24T15:29:37-07:00
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
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
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
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
So clearly there is a bug, at minimum, in the IM 7 processing, since it does not even match that of IM 6.
Re: Compose Mask and Transparency using magick.. -composite
Posted: 2016-05-24T15:40:57-07:00
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.
Re: Compose Mask and Transparency using magick.. -composite
Posted: 2016-05-24T15:45:15-07:00
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.
Re: Compose Mask and Transparency using magick.. -composite
Posted: 2016-05-24T15:51:07-07:00
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.
Re: Compose Mask and Transparency using magick.. -composite
Posted: 2016-05-24T15:55:14-07:00
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
Re: Compose Mask and Transparency using magick.. -composite
Posted: 2016-05-24T15:58:58-07:00
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.
Re: Compose Mask and Transparency using magick.. -composite
Posted: 2016-05-24T16:05:13-07:00
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
So I assumed that the same would happen even if the images were transparent.
Re: Compose Mask and Transparency using magick.. -composite
Posted: 2016-05-24T16:09:18-07:00
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.
Re: Compose Mask and Transparency using magick.. -composite
Posted: 2016-05-24T16:10:29-07:00
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
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
Sorry, I have no idea why my images aren't embedding correctly..
Re: Compose Mask and Transparency using magick.. -composite
Posted: 2016-05-24T16:13:26-07:00
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.