png transparency question

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
prouse

png transparency question

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: png transparency question

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: png transparency question

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
prouse

Re: png transparency question

Post by prouse »

Anthony,

your tip:

Code: Select all

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

Thanks
Post Reply