Image Magick compositing with alpha channel in background

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
newmediajosh
Posts: 3
Joined: 2013-02-11T20:10:08-07:00
Authentication code: 6789

Image Magick compositing with alpha channel in background

Post by newmediajosh »

Hey there; I'm a bit new, so I hope I'm asking this the right way:

I'm trying to create a fundraising thermometer that fills up with hearts; let's say we've raised 50%; the bottom half should show empty hearts--the top half full hearts. There is about a 20 pixel fade between the two.

I have 3 PNG's
1 shows empty hearts with a transparent background
1 shows full hearts with a transparent background
1 is a gradient with no transparency that is automatically generated and controls where the fade happens on the thermometer.

The problem:
I can get it to work, but I lose the transparency on the background image.
OR I can preserve the transparency, but instead of a fade from full to empty, I just get a solid cut-off.

My 3 PNGs can be seen here:
http://wskg.org/pledgeimage/outline.png
http://wskg.org/pledgeimage/full.png
http://www.wskg.org/pledgeimage/overlap_mask.png

I should add that i've been putting all 3 of these on top of another transparent PNG called bg.png and I've been using the following:
convert -background transparent -extent 172x420 pledgeimage/bg.png pledgeimage/outline.png pledgeimage/overlap_mask.png -composite pledgeimage/full.png pledgeimage/overlap_mask2.png -gravity East -composite pledgeimage/current_pledge.png

Can somebody point me in the right direction?

Thanks!

Joshua
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Image Magick compositing with alpha channel in backgroun

Post by fmw42 »

Can you explain what you want the gradient to do to the composite image? I see no "thermometer"? What do you mean by this? Also what is the point of the outline? When I compose it over the full.png image, it looks the same as the full.png image, since the outline is already in the full.png image.

Is this what you want?


convert full.png outline.png -compose over -composite \
\( -clone 0 -alpha off \) \
\( -clone 0 -alpha extract overlap_mask.png -compose multiply -composite \) \
-delete 0 -alpha off -compose copy_opacity -composite show:
newmediajosh
Posts: 3
Joined: 2013-02-11T20:10:08-07:00
Authentication code: 6789

Re: Image Magick compositing with alpha channel in backgroun

Post by newmediajosh »

Sorry, I'll be more clear.

I would like ALL of outline.png to appear and for only the portion of full.png to appear that matches the white (or grey) areas. And the grey areas should be semi-transparent.

It's not literally a thermometer (sorry)--what a I meant by that is that we're using this as a fundraising thermometer where the outlines represent our entire goal and they slowly fill up with full hearts over time. I regenerate the image every few minutes based on the current total--so the mask changes.

Also, I'm pretty new at this and I don't know what the 'show:' command does. I'm used to seeing an output file. I'm running this on a windows web server where I can't use the command line so I've been executing it all through PHP and looking at the output files... So I'm not sure what the command you sent me is supposed to do. I couldn't find 'show:' in the docs...

I'm basically looking to accomplish this (sorry I didn't line the bg up perfectly in Photoshop because I was rushing):
http://www.wskg.org/pledgeimage/sample.png

Thanks for the reply!

Joshua
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Image Magick compositing with alpha channel in backgroun

Post by fmw42 »

This will do what you want, but it has a white background.

convert full.png outline.png overlap_mask.png -alpha off -compose over -composite result.png

show: is to display the output in unix systems without creating an output image.
newmediajosh
Posts: 3
Joined: 2013-02-11T20:10:08-07:00
Authentication code: 6789

Re: Image Magick compositing with alpha channel in backgroun

Post by newmediajosh »

Thank you!

But is there any way to do it without the white background?

That's super important for me...

I'm looking to perserve the transparency...

Joshua
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Image Magick compositing with alpha channel in backgroun

Post by fmw42 »

This should do it. It is unix and to change to windows syntax,
replace \( with (
replace \) with )
replace \ at the end of the line with ^

see
http://www.imagemagick.org/Usage/windows/

Steps:
1. read image
2. negate the mask
3. disable the alpha channel in full.png
4. extract the alpha channel from full.png
5. multiply the mask and alpha channel for full.png
6. put the composite alpha channel back into full.png
7-10. repeat for outline.png
11. delete all temporary images and composite the new full.png and outline.png

convert full.png outline.png overlap_mask.png \
\( -clone 2 -negate \) \
\( -clone 0 -alpha off \) \
\( -clone 0 -alpha extract -auto-level \) \
\( -clone 3 -clone 5 -compose multiply -composite \) \
\( -clone 0 -clone 6 -alpha off -compose over -compose copy_opacity -composite \) \
\( -clone 1 -alpha off \) \
\( -clone 1 -alpha extract -auto-level \) \
\( -clone 2 -clone 9 -compose over -compose multiply -composite \) \
\( -clone 8 -clone 10 -compose over -alpha off -compose copy_opacity -composite \) \
-delete 0-6,8,9,10 -compose over -composite result.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Image Magick compositing with alpha channel in backgroun

Post by snibgo »

The second "-auto-level" in Fred's script makes the outlines in result.png less transparent than they are in outline.png. We can also simplify (Windows script):

Code: Select all

"%IMG%convert" ^
  full.png ^
  ( overlap_mask.png -negate ) ^
  ( -clone 0 -alpha extract -auto-level ) ^
  ( -clone 1 -clone 2 -compose multiply -composite ) ^
  ( -clone 0 -clone 3 -alpha off -compose over -compose copy_opacity -composite ) ^
  -delete 0-3 ^
  outline.png ^
  +swap ^
  -compose over -composite ^
  result3.png
I also note that overlap.png is 419 pixels wide, but the others are 420. It would be better if they matched.
snibgo's IM pages: im.snibgo.com
Post Reply