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

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

Post by svengineer99 » 2016-05-25T13:30:33-07:00

Using -compose src for the overlay does not work for me:

Code: Select all

magick m_bgnd.png m_masked.png -compose src -composite m_over_masked.png
Wipes out the background:
Image

The example images are partially transparent (filled circles with transparent background) right? But actually partially transparent circles are much more useful to look at for overlaying transparencies:

Code: Select all

magick -size 60x60 xc:none -fill rgba(255,0,0,0.5)  -draw "circle 35,21 35,3"  m_src.png
magick -size 60x60 xc:none -fill rgba(0,0,255,0.5) -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_src.png m_mask.png ( -clone 0 -alpha extract ) ( -clone 1 -clone 2 -compose multiply -composite ) -delete 1,2 -alpha off -compose copy_opacity -composite m_masked.png
magick m_bgnd.png m_masked.png -compose over -composite m_over_masked.png
Produces an image with unchanged (0.5) opacity and colors in non-overlapping regions but increased opacity (0.75) and rgb(170,0,85) in the overlapping region:
Image

Dissolve:

Code: Select all

magick m_bgnd.png m_masked.png -compose dissolve -composite m_over_masked.png
or

Code: Select all

magick m_bgnd.png m_masked.png -compose dissolve -define compose:args=50x50 -composite m_over_masked.png
Gives the same result as over. I don't understand either the alpha or color channel behavior. It seems like the overlapping region opacity = 1xbackground+0.5xsource while the color = 2/3xbackground+1/3source. Does that make sense?

Blend gives overlapping region opacity = 1xbackground+1xsource (1.0) while color channels = 0.5xbackground+0.5xsource (170,0,85)

Code: Select all

magick m_bgnd.png m_masked.png -compose blend -composite m_over_masked.png
Image

What I was hoping for in the overlapping region is the simple average for both the opacity and color channels = 0.5xbackground+0.5xsource. Is there any (easy I hope) way to do that?

snibgo
Posts: 11994
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 » 2016-05-25T13:56:17-07:00

svengineer99 wrote:magick m_bgnd.png m_masked.png -compose src -composite m_over_masked.png
Wipes out the background
That's what it should do. The result should be simply m_masked.png. "-compose src" is useful when using a third image, a mask. Where the mask is white you will get m_masked.png, but where the mask is black you will get m_bgnd.png.
svengineer99 wrote:magick m_bgnd.png m_masked.png -compose dissolve -composite m_over_masked.png
"-compose blend" and "-compose dissolve" should both normally be used with "-define compose:args={blah}". See http://www.imagemagick.org/Usage/compose/#dissolve
svengineer99 wrote:What I was hoping for in the overlapping region is the simple average for both the opacity and color channels = 0.5xbackground+0.5xsource. Is there any (easy I hope) way to do that?

Code: Select all

magick m_src.png m_bgnd.png -channel RGBA -evaluate-sequence Mean +channel out.png
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 25383
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 » 2016-05-25T14:30:46-07:00

Using -compose src for the overlay does not work for me:

magick m_bgnd.png m_masked.png -compose src -composite m_over_masked.png
Sorry I thought you had 3 images (background overlay mask). I was confused about what you want to happen. snibgo's solution above is about as simple as it comes, if that result is what you really want.

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 » 2016-05-25T14:39:06-07:00

Thanks snibgo, fmw42,

I'm happy now. Thanks to all your help and tips!

I'm just realizing how complex the overlay behavior is with transparency. The default behavior of over, blend and dissolve seem a bit odd, that's all I was trying to convey in my last post, but I guess there is no 'right' answer. I think I have enough information now to figure things out for my application. If I have more questions I will post again separately.

Thank you both again.

User avatar
fmw42
Posts: 25383
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 » 2016-05-25T15:09:56-07:00

Here are at least three ways to do masking with an image with transparency and keep the original image's transparency along with the transparency from the mask image

Image:
Image

Mask:
Image


1)
line1: read img and mask images
line2: extract the alpha channel from the img
line3: multiply the mask by the extracted alpha channel
line4: delete temporary files
line5: put the new product alpha channel into img
line6: write output

Code: Select all

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


2) simplified using mpr rather than clones
line1: read img and mask images and convert mask into mpr format deleting the mask.png
line2: multiply the extracted alpha channel by the mpr:mask
line3: put the new product alpha channel into img
line4: write output

Code: Select all

convert img.png \( mask.png -write mpr:mask +delete \) \
\( -clone 0 -alpha extract mpr:mask -compose multiply -composite \) \
-alpha off -compose copy_opacity -composite \
result2.png
Image


3)
line1: read img
line2: read mask images and convert mask into mpr format deleting the mask.png
line3 and 4: convert the mask into a white image with the mask as the alpha channel
line5: multiply the img and new mask image including multiplying the alpha channels
line6: write output

Code: Select all

convert img.png \
\( mask.png -write mpr:mask +delete \
mpr:mask -fill white -colorize 100 mpr:mask \
-alpha off -compose copy_opacity -composite \) \
-compose over -channel rgba -alpha on -compose multiply -composite \
result3.png
Image

User avatar
fmw42
Posts: 25383
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 » 2016-05-26T10:25:32-07:00

Here is a 4th method, which seems to be the most direct and simplest

Line1: read img.png
Line2: read mask.png, copy the image to the alpha channel, then set the rgb channels to white
Line3: multiply img.png with the modified mask image
Line4: write the output

Code: Select all

convert img.png \
\( mask.png -alpha copy -channel rgb -evaluate set 100% +channel \) \
-channel rgba -alpha on -compose multiply -composite \
result4.png
Image

snibgo
Posts: 11994
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 » 2016-05-26T10:44:52-07:00

In your result4, you multiply each of the RGBA channels, after setting RGB to 100%. So you only need to multiply channel A, so you don't need to set RGB to 100%.

Code: Select all

convert img.png ( mask.png -alpha copy ) -channel a -alpha on -compose multiply -composite result5.png
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 25383
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 » 2016-05-26T11:44:11-07:00

Snibgo, very nice. Thanks. I had tried something similar, but unsuccessfully, earlier, but had left off the -alpha copy. I thought I could multiply the mask by the alpha channel directly. But I see that I needed to promote the image to the alpha channel first.

But formally, would it not be more proper to add +channel before writing the result. If I ever select a channel, I always +channel afterwards before saving the image. I have had some problems in IM 6 before with not doing so, especially if further processing is desired before writing the result. More of a good practice, even if not needed in this case.

Code: Select all

convert img.png \
\( mask.png -alpha copy \) \
-channel a -alpha on -compose multiply -composite +channel \
result5.png
Image

snibgo
Posts: 11994
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 » 2016-05-26T11:53:11-07:00

fmw42 wrote:But formally, would it not be more proper to add +channel before writing the result.
Yes, that's certainly true. My only excuse is that you didn't, so I didn't either.
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 25383
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 » 2016-05-26T11:55:44-07:00

snibgo wrote: Yes, that's certainly true. My only excuse is that you didn't, so I didn't either.
I think I did not only because the latter few cases had -channel rgba, so all channels were in effect.

snibgo
Posts: 11994
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 » 2016-05-26T11:57:45-07:00

Ah, true.
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 25383
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 » 2016-05-26T12:01:12-07:00

Between you and I, it has been fun trying to reduce this problem down to the simplest solution until an proper implementation of something like -compose multiply_opacity.

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 » 2016-05-26T12:53:42-07:00

Thanks for all the alternate methods. For future proofing the scripts I am working on now, do you think the 4th (simplest) method has at least as good of chance as the others of remaining working with future IM revision/release?

User avatar
fmw42
Posts: 25383
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 » 2016-05-26T13:20:43-07:00

If it works on the latest versions of both IM 6 and IM 7, then barring some odd temporary bug creeping in, it should be stable for future release. But right now on the current versions of IM, only result.png and result2.png methods work in IM 7. They all work in IM 6. Once this bug is fixed in IM 7, then all the rest of the methods should be fine.

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 » 2016-05-26T13:30:37-07:00

Thanks! I will stick with method 1 (on IM7) for now.

Post Reply