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

Composing Transparent Images

Post by dognose » 2019-07-29T05:07:15-07:00

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: 12418
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Composing Transparent Images

Post by snibgo » 2019-07-29T05:22:04-07:00

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

Re: Composing Transparent Images

Post by dognose » 2019-07-29T06:37:59-07:00

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: 12418
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Composing Transparent Images

Post by snibgo » 2019-07-29T06:39:09-07:00

Please post your input images.
snibgo's IM pages: im.snibgo.com

dognose
Posts: 267
Joined: 2005-03-08T22:16:37-07:00

Re: Composing Transparent Images

Post by dognose » 2019-07-29T06:54:59-07:00

Here you go

Image

Image

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

Re: Composing Transparent Images

Post by snibgo » 2019-07-29T07:28:50-07:00

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

Re: Composing Transparent Images

Post by dognose » 2019-07-29T07:36:22-07:00

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

Post Reply