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?".
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 »

The first is the image and second is the mask. So this should be the one that works.

Code: Select all

magick 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
Since you are using IM 7, change convert to magick.
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 had hoped that functionally masking would be the same whether it was transparent or not.
Yes, it should be.

Let me put this another way. Take two commands, assuming dst.png src.png are the same sizes:

Code: Select all

convert dst.png src.png -compose XX -composite out.png

convert dst.png src.png ( +clone -fill White colorize 100 ) -compose XX -composite out.png
The commands are the same, except that the second command is masked, but the mask is entirely white. The results from these two commands should be identical (provided the compose settings XX are the same).
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 wrote:
Let me put this another way. Take two commands, assuming dst.png src.png are the same sizes:

Code: Select all

convert dst.png src.png -compose XX -composite out.png

convert dst.png src.png ( +clone -fill White colorize 100 ) -compose XX -composite out.png
The commands are the same, except that the second command is masked, but the mask is entirely white. The results from these two commands should be identical (provided the compose settings XX are the same).
But, what it produces for non-transparent images is just src.png. So I assumed that the same would happen for transparent images.

But for transparent image, I get a red circle overlaying the blue one on a transparent background.

OK. I see the functionality now for this example. It takes dst.png and composites src.png over it, but only where mask is white.

My misunderstanding because I assumed the transparent case would take only the part of dst.png that corresponded to where mask is black, rather than taking all of dst.png.

Functionally, I thought it worked by taking dst only where mask is black and src only where mask is white. This is the result when non-transparent. But I see that it is a limiting case of taking all of dst and src only where mask is white when non-transparent.

Thanks for the clarification.

It would be nice to have a -compose method that operated the way I thought it did as per the result of my long command line. That is take dst where mask is black and src where mask is white whether transparent or not.
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:It would be nice to have a -compose method that operated the way I thought it did as per the result of my long command line. That is take dst where mask is black and src where mask is white whether transparent or not.
"-compose Src" should do exactly that. In this case, we will get two half-circles. In v6.9.2-5, that's what we get.
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 »

Yes, that does indeed work. Thanks. I will have to remember that. That is not readily apparent to me from the Duff-Porter methods, since Anthony's pages do not show masking examples for them.
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 »

See http://www.imagemagick.org/Usage/compose/#mask_trans . That's an old page, so it uses "composite" rather than "convert".
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 »

Thanks. I missed that since it was down the page further.

I am finally getting a better understanding of the Duff-Porter methods, now, since I have rarely used anything but over.
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 wrote:The first is the image and second is the mask. So this should be the one that works.

Code: Select all

magick 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
Since you are using IM 7, change convert to magick.
Unfortunately, this is not working for me on IM7, output image seems gone crazy:
Image
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 »

Sorry, my typo. In the second parens, it should be -clone 1 -clone 2, not -clone 1 -clone 0. I am taking the alpha channel from the first image and multiply by the mask image, then replacing the alpha channel in the first image with the product image, deleting temporary files along the way.

try

Code: Select all

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 result.png
where m_src.png is your transparent color image and m_mask.png is the black/white mask image.
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 »

That worked! Thank you!! I can use this right now!..

Although, it would be nice if something like the prior working simple method could work again in the future. Would you recommend to submit a separate bug post about it, linking to this thread, or something else?
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 »

Which code do you mean? I am not sure what you hope to achieve in your first example. Can you explain what you want for the output result?

In IM 6.9.4.4, this works

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
Do you want this?

Code: Select all

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

Or this (thanks to snibgo)?

Code: Select all

convert m_bgnd.png m_src.png m_mask.png -compose src -composite m_over_masked_6944_src.png
Image

In IM 7 there is a bug, which I have reported at viewtopic.php?f=3&t=29762

If you want something else, then please clarify further and describe what the output should look like.
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 »

The first one (masked partially transparent source over unmasked partially transparent background) would be much more useful to me than the second (masked partially transparent source over masked partially transparent background). Thank you and snibgo again for all your help!
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 »

You can also do this if you leave the mask off:

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

Code: Select all

convert m_bgnd.png m_src.png -compose over -composite m_over_masked_6944_over_nomask.png
Image
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 »

Great. So referring back to the v6 Compose Mask and Transparency example http://imagemagick.org/Usage/compose/#mask, I think the simplest work around for v7 should be to replace:

Code: Select all

composite m_src.png  m_bgnd.png   m_mask.png      m_over_masked.png
with

Code: Select all

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
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 »

The images in the linked example are opaque and the example and code work fine for that. The problem occurs when you have transparent images. That needs to be handled differently. It is not simply the default -compose over. In fact, it is more consistent to do both transparent image and non-transparent images using -compose src (as I now understand it). The second set of code can be simplified by using -compose src, so that it behaves similarly to the first example. See all the examples of the red and blue circles above. Decide how you want the transparency to be handled and use the appropriate method. I do not believe that it is an issue of IM 7 vs IM 6. IM 7 has a bug and once that is fixed, they both will behave the same so that the appropriate shorter code will work.
Post Reply