Creating a semi-transparent PNG with ImageMagick on Centos Linux

老子叫甜甜 提交于 2019-12-03 13:12:18

问题


I have PNG files that I would like to convert the whole image to be semi-transparent. The image will be referenced in a KML file as an icon overlay for use in Google Earth/Maps.

The following examples have been suggested to me using the ImageMagick convert command, however neither seems to work. The first example results in an error, the second example fails to make any observable change to the image.

$ /usr/local/bin/convert 121112142121.png -channel Alpha -evaluate Set 50% 2121112142121.png
convert: no decode delegate for this image format '121112142121.png' @ error/constitute.c/ReadImage/550.
convert: option requires an argument '-evaluate' @ error/convert.c/ConvertImageCommand/1400.

The second suggested method was this:

convert filename.png -fill '#00000080' -draw 'matte 100,100 reset' output.png

While I can find many examples on the web for creating fully transparent images from specific colors or alpha masks, I cannot seem to find any that work for creating semi-transparent images. I would like to find a way to do this with the convert command, or with ImageMagick Perl.

ImageMagick version is 6.8.0-4.


回答1:


Resolution has been found:

convert input.png -alpha set -channel A -evaluate set 50% output.png

The above command makes the entire image (all colors) semi-transparent.

Another problem I had was that the latest version of ImageMagick was compiled from source without all the most recent image libraries installed (in particular libpng). Pay close attention to the output of configure to ensure libpng is found if compiling from source. It also appears that versions of ImageMagick earlier than 6.6 and/or older versions of libpng may not support transparent png generation.




回答2:


convert input.png -alpha set -channel A -evaluate Divide 2 output.png

Thank you @caw for pointing this out in your comment. This helps in the case where the image already has some transparent pixels. -evaluate set 50% ruins the already-transparent pixels that had alpha 0, setting their alpha to 50%, which is often not desirable.

-evaluate Divide 2 preserves the alpha, since fully transparent pixels are alpha 0, and 0 divided by 2 is still 0.




回答3:


To followup on Tony Bogdanov's comment. If you already have fully transparent pixels you may want to use the following strategy:

1.) convert the transparent pixels into a mask

convert -alpha extract input.png mask.png

2.) perform the command listed in the answer above:

convert input.png -alpha on -channel a -evaluate set 65% output.png

3.) create a blank canvas the same size as the original image

example: convert -size 700x800 xc:none blankcanvas.png

4.) composite the semitransparent image and the blank canvas together using the transparent pixel mask

composite output.png blankcanvas.png mask.png -compose Src final_output.png



来源:https://stackoverflow.com/questions/13366273/creating-a-semi-transparent-png-with-imagemagick-on-centos-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!