使用ImageMagick/convert创建一个半透明的PNG图片

12

我有一些PNG文件,希望将整个图像转换为半透明。

该图像将被引用为KML文件中的图标覆盖层,以在Google Earth / Maps中使用。

以下是使用ImageMagick convert命令向我建议的示例,但似乎都不起作用。

第一个示例会导致错误,

$ /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.

第二个未能对图像进行任何可观察的改变:

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

虽然我可以在网上找到很多例子来创建特定颜色或alpha蒙版的完全透明图像,但我似乎找不到任何可用于创建半透明图像或更改不透明度的示例。我希望能够找到一种使用convert命令或ImageMagick Perl实现这个目标的方法。

(ImageMagick版本为6.8.0-4)

3个回答

14

已找到解决方案:

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

上述命令使整个图像(所有颜色)半透明。

我遇到的另一个问题是,最新版本的ImageMagick是从源代码编译而成的,但并没有安装所有最新的图像库(特别是libpng)。如果从源代码编译,请密切关注configure的输出以确保找到libpng。此外,似乎早于6.6版本的ImageMagick和/或旧版的libpng可能不支持生成透明的PNG图片。


9
如果您想使图像处理过程独立于原始图像,也可以使用“-evaluate Divide 2”(将所有像素的透明度加倍)。 - caw
6
这种方法的问题在于,如果图像中已经有完全透明的像素,则它们的透明度似乎也会改变,使它们变成半透明黑色 :( 有什么想法吗? - Tony Bogdanov

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

感谢@caw在评论中指出这一点。这对于图像已经有一些透明像素的情况很有帮助。-evaluate set 50%会破坏已经具有alpha 0的透明像素,将它们的alpha设置为50%,这通常是不可取的。 -evaluate Divide 2可以保留alpha,因为完全透明的像素是alpha 0,而0除以2仍然是0。

我发现如果不加上“-matte”标志,图像会出现像素化。 - Jonathan Allard

1

回应Tony Bogdanov的评论。如果您已经拥有完全透明的像素,则可以使用以下策略:

  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
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接