Page 1 of 1

Partial Transparency lost on composite

Posted: 2013-08-22T12:59:20-07:00
by superdav42
I seem to be running into a problem similar to this one but the sample images are no longer available and the problem wasn't resolved.
viewtopic.php?f=1&t=21661

I'm creating images overlayed on a background with an alpha mask applied. Sometimes I'm using a jpg for a background and sometimes I need it to be a transparent background. Using a jpeg works fine but using an empty transparent file with xc:none produces jagged edges instead of the smooth transition that is in the mask.

Here's the command:

Code: Select all

convert  -size 1024x768 xc:none    vader.jpg  \( 'alpha.jpg' -negate  \) -composite   'output_broken.png'
It looks great with:

Code: Select all

convert  -size 1024x768 xc:white    vader.jpg  \( 'alpha.jpg' -negate  \) -composite   'output.png'
but I want the background to be transparent instead of white.

I'm using ImageMagick 6.7.7-10 2013-02-25 Q16 on Ubuntu 13.04

How can I make the transparent version look like the white background? It's like it's not using png32 or something.

sample of output_broken.png:
Image
output with white background:
Image
alpha mask:
http://i.imgur.com/oh7exKw.jpg
vader input image:
http://i.imgur.com/xiDRKhO.jpg

Re: Partial Transparency lost on composite

Posted: 2013-08-22T14:09:15-07:00
by snibgo
Your mask doesn't cover the area shown in your example. I'll ignore that problem.

I'd do it this way:

Code: Select all

convert vader.jpg ( vader_mask.jpg -negate ) -compose CopyOpacity -composite o.png
The reslut is larger than you asked for; you can crop it.

Re: Partial Transparency lost on composite

Posted: 2013-08-22T16:23:08-07:00
by superdav42
I'd really like to use the same syntax for the transparent background as a regular background since this command is being generated programatically. However I'll use what ever works.
The command you gave is slightly different than the desired output.
My result with it:

Code: Select all

convert -size 1024x768 vader.jpg \( ~/mask.jpg -negate \) -compose CopyOpacity -composite -crop 1024x768 o2.png
o2:
Image
As you can see the visible area is much smaller that the above which is what I'm trying to achieve.

Should the first command work, are the jagged edges a bug?

Re: Partial Transparency lost on composite

Posted: 2013-08-22T16:38:53-07:00
by fmw42
Assuming you have two images the same size and the background has transparency and the second is an alpha mask, then you need to extract the alpha channel of the background image and multiply it with your mask, then replace the alpha channel of the background with multiplied mask. For example lets start with the IM internal logo: image

display logo:

Now make it transparent

convert logo: -transparent white logot.png

display logot.png

Now make a mask image such that the top right quadrant is black and the rest white

convert -size 640x480 xc:white -size 320x240 xc:black -gravity northeast -compose over -composite logo_mask.png

display logo_mask.png


Now process them together to make the output transparent in the top right corner without affecting the transparency in the rest of the image

convert logot.png logo_mask.png \( -clone 0 -alpha extract \) \
\( -clone 1 -clone 2 -compose multiply -composite \) -delete 1,2 -alpha off \
-compose copy_opacity -composite result.png

Re: Partial Transparency lost on composite

Posted: 2013-08-22T19:10:25-07:00
by snibgo
superdav42 wrote:Should the first command work, are the jagged edges a bug?
It's just the way that composition works, where colour values are pre-multiplied by alpha. See http://www.imagemagick.org/Usage/compose/ . Compositing where the destination has transparency can be counter-intuitive. Your jaggies are the border of white on your mask.