ImageMagick, replace semi-transparent white with o

2019-07-21 16:01发布

问题:

I have an icon with a fully transparent background and a semi-transparent, white foreground. I would like to make the foreground fully opaque, can this be achieved with ImageMagick?

I have tried juggling different combinations of these;

http://www.imagemagick.org/discourse-server/viewtopic.php?t=12619

http://www.imagemagick.org/discourse-server/viewtopic.php?t=18196

http://www.imagemagick.org/discourse-server/viewtopic.php?t=16718

, but cannot produce the desired result. Any tips?

回答1:

It would be easier if you posted your icon, but my testing shows that this works for what I think you have:

convert icon.png -channel A -threshold 75% output.png

The above is somewhat coarse as it makes all partially transparent pixels fully opaque. If you wanted to be a bit more surgical, you could only set the opacity to fully opaque when the Red, Green and Blue pixels are greater than 90% and the original opacity (alpha) is between 40%-60% like this:

convert icon.png -channel A \
   -fx "(r>0.9 && g>0.9 && b>0.9 && a>0.4 && a<0.6) ? 1.0 : a" output.png