Composing Transparent Images

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
dognose
Posts: 265
Joined: 2005-03-08T22:16:37-07:00

Composing Transparent Images

Post by dognose »

Hi,
I'm having trouble blending/composing two transparent png images

I'm using: (though I've tried a few other ways too)
composite -compose over -geometry +10+10 top.png background.png output.png

The output is mostly right, but the top image loses it's alpha transparency.

Is there a way to blend the top image into the background?

I've looked at -layers merge, but then the transparency of the base image cuts out the overlay..
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Composing Transparent Images

Post by snibgo »

What version of IM, on what platform?

I suggest you don't use the "composite" program. Instead use "magick" (for IM v7) or "convert" (for IM v6). With those, use the "-composite" operator, and list the two inputs in the opposite order:

Code: Select all

convert background.png top.png -geometry +10+10 -compose Over -composite output.png
Does that do what you want? The result alpha at each pixel depends on the alphas of both inputs.

Perhaps you want "-compose Blend".
snibgo's IM pages: im.snibgo.com
dognose
Posts: 265
Joined: 2005-03-08T22:16:37-07:00

Re: Composing Transparent Images

Post by dognose »

I'm still getting the same thing. Alpha on the overlay image is lost..
I'm using im 6.7.2

This is what I'm getting (with grid added to show transparency)

Image

Vs. this is what I want

Image


If I try to use the blend compose it doesn't compose properly
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Composing Transparent Images

Post by snibgo »

Please post your input images.
snibgo's IM pages: im.snibgo.com
dognose
Posts: 265
Joined: 2005-03-08T22:16:37-07:00

Re: Composing Transparent Images

Post by dognose »

Here you go

Image

Image
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Composing Transparent Images

Post by snibgo »

V6.7.2 is very old. It may have a bug. Even if it doesn't, I suggest you upgrade.

If you want the output size to be the same as the first input, use "-composite":

Code: Select all

magick DQk2r9I.png Ws9m2Ky.png -geometry +200+10 -compose Over -composite circs2a.png
Image

If you want the output size to be the smallest that contains all the input images, use "-layers merge". This needs one input to be repaged. If you don't want to flatten against a background colour, you need "-background none".

Code: Select all

magick DQk2r9I.png ( Ws9m2Ky.png -repage +200+10 ) -background None -compose Over -layers merge circs2b.png
Image
snibgo's IM pages: im.snibgo.com
dognose
Posts: 265
Joined: 2005-03-08T22:16:37-07:00

Re: Composing Transparent Images

Post by dognose »

Thanks, I was afraid of that, as it breaks some other scripts I use.
Post Reply