how to change the opacity value of a png file?

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
linjuming
Posts: 122
Joined: 2011-01-25T00:09:50-07:00
Authentication code: 8675308

how to change the opacity value of a png file?

Post by linjuming »

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

Re: how to change the opacity value of a png file?

Post by fmw42 »

Please provide a link to the image you want to change? I don't know if you are trying to change a constant opacity in the image or a gradient opacity in the image? Please clarify.

see transparent canvas at http://www.imagemagick.org/Usage/canvas/#other

especially

convert test.png -matte -channel A -evaluate set 0 +channel trans_evaluate.png


change the set value to some value between 0 and 1
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to change the opacity value of a png file?

Post by fmw42 »

if this is to change a gradient transparency from 0-100% to 0-70%, then try

convert test.png -matte -channel A +level 0,70% +channel trans_evaluate.png

if this is the wrong way opacity vs transparency, then use +level 30,100%
linjuming
Posts: 122
Joined: 2011-01-25T00:09:50-07:00
Authentication code: 8675308

Re: how to change the opacity value of a png file?

Post by linjuming »

original png
Image

code:

Code: Select all

<?php
	exec("convert opacity.png -matte -channel A -evaluate set 0.5 +channel laji.png");
	echo "<img src='opacity.png'/>";
	echo "<img src='laji.png'/>";

?>
<style type="text/css">
	html{background-color:gray;}
</style>
preview in firefox
Image

why the png is completly transparent ?
linjuming
Posts: 122
Joined: 2011-01-25T00:09:50-07:00
Authentication code: 8675308

Re: how to change the opacity value of a png file?

Post by linjuming »

not to change the contract , I only want to chage the png's opacity ,from 100% to 74% ,such as what i showed in photoshop above .but your code output a completly transparent png . Why?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: how to change the opacity value of a png file?

Post by anthony »

I'm suprised there isn't a broken image!

Can the PHP write in that directory. is there a transparent image in that directory that is not writable by the PHP?

Better to check these thing without the complication of web page processing.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to change the opacity value of a png file?

Post by fmw42 »

please provide a link to your png image (not in a screen snap) so we can see what is really happening.
linjuming
Posts: 122
Joined: 2011-01-25T00:09:50-07:00
Authentication code: 8675308

Re: how to change the opacity value of a png file?

Post by linjuming »

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

Re: how to change the opacity value of a png file?

Post by fmw42 »

Your png file is composed of a white base image and a gradient alpha channel. So there is no one alpha value in that file. So what is that you want to do. If you make the opacity/transparency a constant, then you will lose your gradient.

To see this, look at the results of:

convert opacity-2.png -alpha off opacity-2_aoff.png
convert opacity-2.png -alpha extract opacity-2_alpha.pn



You would be better making a gradient base image and then making the alpha channel some constant say 70% as was describe above.

Try this:

convert -size 128x128 gradient: -matte -channel A -evaluate set 70% -channel rgba tmp.png

or

convert -size 128x128 gradient: -matte -channel A -evaluate set 30% -channel rgba tmp.png


Not sure which way you want it.
linjuming
Posts: 122
Joined: 2011-01-25T00:09:50-07:00
Authentication code: 8675308

Re: how to change the opacity value of a png file?

Post by linjuming »

Thank you , it helps
Post Reply