Level of transparency

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
koxon
Posts: 4
Joined: 2014-04-19T16:26:55-07:00
Authentication code: 6789

Level of transparency

Post by koxon »

Hi all,

I need to change the opacity or alpha of my image based on a float variable from 0.0 to 1.0.
0 = transparent, 0.5 = half transparent and 1 = opaque.
This is a classic function that you find in many softs and also Imagic for PHP. See: http://www.php.net/manual/en/imagick.se ... pacity.php

I can't seem to find how to do it with the command line tool "convert".
Here is what I tried:
$> convert logo.png -alpha on -channel A -evaluate divide 3 result.png
This works fine but as I do a "divide" I can't reach 0 and create full transparency. It also hard for me to translate my input variable 0>1 to the proper division factor.
Image

$> convert logo.png -alpha on -channel A -evaluate set 30% result.png
This sounded great! ... However the quality obtain is not. The color is bleeding, I get a black BG and the quality is not good at all. See below:
Image

Do you guys have a solution for me?
I need to be able to convert my float input variable 0>1 to a parameter that "convert" will understand to perform transparency.

Thanks a lot!

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

Re: Level of transparency

Post by fmw42 »

Post your original image.

You need to turn on all the channels after selecting to process only the alpha channel.

Try

convert image -alpha on -channel a -evaluate set 30% +channel result

or

convert image -alpha on -background black -alpha background -channel a -evaluate set 30% +channel result
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Level of transparency

Post by snibgo »

And, of course, you can multiply instead of divide:

Code: Select all

convert logo.png -alpha on -channel A -evaluate multiply 0.25 +channel result.png
snibgo's IM pages: im.snibgo.com
koxon
Posts: 4
Joined: 2014-04-19T16:26:55-07:00
Authentication code: 6789

Re: Level of transparency

Post by koxon »

Thanks for the help guys!
I will try that out.

Best,
koxon
Posts: 4
Joined: 2014-04-19T16:26:55-07:00
Authentication code: 6789

Re: Level of transparency

Post by koxon »

fmw42 wrote:Post your original image.

You need to turn on all the channels after selecting to process only the alpha channel.

Try

convert image -alpha on -channel a -evaluate set 30% +channel result

or

convert image -alpha on -background black -alpha background -channel a -evaluate set 30% +channel result
This makes the background transparent but the quality is not there. I got pixels bleeding on the edges ...
Thanks though.
koxon
Posts: 4
Joined: 2014-04-19T16:26:55-07:00
Authentication code: 6789

Re: Level of transparency

Post by koxon »

snibgo wrote:And, of course, you can multiply instead of divide:

Code: Select all

convert logo.png -alpha on -channel A -evaluate multiply 0.25 +channel result.png
This is the solution!
Works perfect, and instead of Divide you use Multiply! Clever, thank you!
That allow me to use 0-1 values for my level of transparency.

Thanks a lot!
Post Reply