PNG opacity in composition

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
christiaan
Posts: 13
Joined: 2013-08-09T01:20:06-07:00
Authentication code: 6789

PNG opacity in composition

Post by christiaan »

I'm creating a composition of images with methods as described here: viewtopic.php?f=1&t=23883. For each layer I create separate image first (according all selected options in app), and then I'm merging those in one image.

The problem is with text images. For a text I create a transparent PNG image with the text using IM. The text needs to be placed over the image at a certain position, but can be transparent at 60% for instance. For normal JPEG's covering each other, the '-alpha on -channel A -evaluate set 60%' rule was working fine. Now for the transparent PNG files I get a 60% transparent black box. What am I missing here?

Code: Select all

convert
   -size 1457x1445 xc:white -gravity center
   ( "image1.jpg" -gravity center -virtual-pixel None +distort SRT '0' -geometry -0+0 ) -composite
   ( "image2.jpg" -gravity center -virtual-pixel None +distort SRT '0' -geometry -0+0 ) -composite
   ( "text.png" -alpha on -channel a -evaluate set 40% -gravity center -virtual-pixel None +distort SRT '0' -geometry -0+0 ) -composite
    -format JPEG "output.jpg"
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: PNG opacity in composition

Post by snibgo »

You command has "-evaluate set 40%". I would expect this to give 60% transparency. What is your shell? If Windows BAT, you need to double the %.

If this doesn't help, please provide sample input and output images. And say what IM version you are using, on what platform.
snibgo's IM pages: im.snibgo.com
christiaan
Posts: 13
Joined: 2013-08-09T01:20:06-07:00
Authentication code: 6789

Re: PNG opacity in composition

Post by christiaan »

Thanks snibgo. I know but that's not the problem. The image of the text is a full black box (of the image boundaries) at the opacity given with "-evaluate set 40%". The text is completely gone.

Here are some input images:
Image: https://dl.dropboxusercontent.com/u/303 ... /image.jpg
Text: https://dl.dropboxusercontent.com/u/303 ... m/text.png

I hope you can help me or point me in the right direction.

Code: Select all

$ convert -version
Version: ImageMagick 6.8.6-6 2013-08-05 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2013 ImageMagick Studio LLC
Features: DPC Modules
Delegates: bzlib fftw fontconfig freetype gslib jng jpeg lcms lzma png ps tiff x xml zlib
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: PNG opacity in composition

Post by snibgo »

text.png contains opaque black text on a transparent black background. If you set the entire image to a certain transparency, the text and background become identical.

Instead of setting an alpha, you could multiply it. Instead of "-evaluate set 40%", use "-evaluate multiply 0.4". That way, the text and background will have different alpha.
snibgo's IM pages: im.snibgo.com
christiaan
Posts: 13
Joined: 2013-08-09T01:20:06-07:00
Authentication code: 6789

Re: PNG opacity in composition

Post by christiaan »

Thanks snibgo, that seems to work! But is multiply giving the same results as normal opacity? I don't need special- or blending effects.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: PNG opacity in composition

Post by snibgo »

Sorry, I don't understand you. What is "normal opacity"?

text.png has text with opacity 1.0 and background opacity 0.0. If you set the image opacity to 40%, both text and background have opacity 0.4. As they are the same colour (black), the text is not visible.

If you multiply opacity by 0.4, the background remains zero (because 0 * 0.4 = 0), but the text becomes 0.4 (because 1 * 0.4 = 0.4).
snibgo's IM pages: im.snibgo.com
christiaan
Posts: 13
Joined: 2013-08-09T01:20:06-07:00
Authentication code: 6789

Re: PNG opacity in composition

Post by christiaan »

Ah, it's multiply in that way. I was thinking of blending options like those in Photoshop. Never mind, I get it now.

The text is not always black, it can be any color. Will this code also work than?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: PNG opacity in composition

Post by snibgo »

For different compositing methods, such as LinearLight or even one called Multiply, use "-compose X".

Yes, this will work for any colour. If it had been red instead of black, you would have seen more clearly what was happening.
snibgo's IM pages: im.snibgo.com
Post Reply