Page 1 of 1

png transparency question

Posted: 2008-02-19T11:14:41-07:00
by prouse
I have several thousand png images that contain multiple colors. I would like to simply make all the entire image 50% transparent/opaque. I can see how you can make specific colors of an image transparent using the -transparent switch but, I cannot see how you would simply make the entire image a certain percentage transparent. I've been using fireworks to do this, but it is pretty slow with the number of images I have to work with.

Let me know

Thanks

Re: png transparency question

Posted: 2008-02-19T19:11:04-07:00
by fmw42
How are you determining what 50% is to be transparent?

You could use -black-threshold to put all pixels that are the bottom 50% graylevel to black leaving the rest as is. Then use -transparent black to make all the thresholded pixels go transparent.

convert <infile> -channel RGB -black-threshold 50% -transparent black <outfile>

This makes all pixels below 50% transparent but does not ensure that 50% of the pixels become transparent. That was why I was asking how you are determining what 50% of the data needs to be made transparent.

If you want to make 50% of the data randomly transparent, then you need to create a random noise mask using +noise with a middle gray image (xc:gray) and threshold it to be 50% white and 50% black. Then use -compose multiply composite to mask your image. Then make the black pixels transparent. Something like

convert <infile> \( -size widthxheight xc:gray +noise random -threshold 50% \) -compose multiply -composite -transparent black <outfile>

where widthxheight is the size of <infile>

I have not tested either of these. So let us know what happens or if you are specifying your 50% in some other way.

P.S. I just found that +noise is not apparently working correctly in my version of IM 6.3.8-6 Q16. I have reported it to Bugs.

P.S. 2 This bug in +noise is fixed in IM 6.3.8-10.

Re: png transparency question

Posted: 2008-02-20T18:24:23-07:00
by anthony
there are many ways to make all pixels in a image 50% more transparent than they were.

The simplest is using -dissolve or -blend in "composite"
http://imagemagick.org/Usage/compose/#blend_use

The more direct method is to use -evaluate with a -channel restriction to adjust the alpha channel.

Code: Select all

   convert image.png -channel A -evaluate divide 2 +channel image_semitrans.png
See http://imagemagick.org/Usage/transform/#evaluate
For JPEG source images you will also need a -matte
before the -evaluate to ensure a matte/alpha channel exists.

Re: png transparency question

Posted: 2008-02-21T08:19:50-07:00
by prouse
Anthony,

your tip:

Code: Select all

convert image.png -channel A -evaluate divide 2 +channel image_semitrans.png
worked perfectly!

Thanks