[SOLVED] Combining multiple masks/colors to generate image

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?".
Post Reply
ivant

[SOLVED] Combining multiple masks/colors to generate image

Post by ivant »

Hi,

In a web-based app I'm developing, I need to recolor some images, using the customer specified colors. My take on this was, that the designers will create a series of masks and I'll paint them with the respective color and combine the results. The masks will be the same size, so I don't have to worry about how to combine them. I also wanted to do that without generating intermediate images on the disk, because I'll have to clean-up afterwards.

The problem is, that I can't understand how to combine the commands.

For single mask, I do this:

Code: Select all

convert -size {WxH} \
   \( xc:{color1} {mask1} +matte -compose Copy_Opacity -composite \) \
   {result}
For two masks, this:

Code: Select all

convert -size {WxH} \
   \( xc:{color1} {mask1} +matte -compose Copy_Opacity -composite \) \
   \( xc:{color2} {mask2} +matte -compose Copy_Opacity -composite \) \
   -compose Src_Over -composite \
   {result}
However, this doesn't work for three masks:

Code: Select all

convert -size {WxH} \
   \( xc:{color1} {mask1} +matte -compose Copy_Opacity -composite \) \
   \( xc:{color2} {mask2} +matte -compose Copy_Opacity -composite \) \
   \( xc:{color3} {mask3} +matte -compose Copy_Opacity -composite \) \
   -compose Src_Over -composite \
   {result}
Please, help me.

Cheers,
Ivan

P.S. The application is written in C++ and I'd rather have it call an external shell script to do the work, than to link it to IM.
Last edited by ivant on 2008-01-18T06:48:27-07:00, edited 1 time in total.
ivant

Re: Combining multiple masks/colors to generate image

Post by ivant »

While waiting for an an answer I decided to play a bit with the script using intermediate images. As I said I prefer not to, but it's better to have something working. So here is what I did:
  • I prepared 3 image masks, each one containing an ellipse.
  • Colored the masks in red, green and blue respectively, like this:

    Code: Select all

    convert -size {size} xc:red mask1.png +matte -compose Copy_Opacity -composite r.png
    convert -size {size} xc:green mask2.png +matte -compose Copy_Opacity -composite g.png
    convert -size {size} xc:blue mask3.png +matte -compose Copy_Opacity -composite b.png
  • Combined the results in one image:

    Code: Select all

    convert r.png g.png b.png -composite res.png
The result was the same as with the single command line from the previous script! That is, red ellipse on top of green one, semi-transparent where they "collide". The blue ellipse is not present! Then I tried to combine just the red and the green:

Code: Select all

convert r.png g.png -composite rg.png
and it worked fine. But adding the blue one generated the same bad result:

Code: Select all

convert rg.png g.png -composite rgb.png
So, it looks like I understand IM even less than I thought (which wasn't much anyways).

Any ideas how to do that right?

P.S. Should I post the test images somewhere?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Combining multiple masks/colors to generate image

Post by Bonzo »

Is this the sort of thing you are trying to do?
http://www.imagemagick.org/Usage/compose/#bumpmap

Also a link to the images and an example of the final image you are looking to produce would help.
ivant

Re: Combining multiple masks/colors to generate image

Post by ivant »

Bonzo wrote:Is this the sort of thing you are trying to do?
http://www.imagemagick.org/Usage/compose/#bumpmap

Also a link to the images and an example of the final image you are looking to produce would help.
Actually, what I needed was like this: http://www.imagemagick.org/Usage/compose/#plus, but with "over" instead of "plus". And it worked fine! Thanks. And I've actually seen it before! :shock: I feel so stupid.

Also, looking at the top of the same page it's now clear that the command:

Code: Select all

convert 1.png 2.png 3.png -composite r.png
won't put 3 on top of 2 on top of 1 (as I was expecting), but will treat 3 as a mask and do something different with it. The correct command to do that is:

Code: Select all

convert \( 1.png 2.png -composite \) 3.png -composite r.png
Again, thank you very much!
Post Reply