【问题标题】:Detect if an image is transparent in GraphicsMagick在 GraphicsMagick 中检测图像是否透明
【发布时间】:2017-02-15 23:25:34
【问题描述】:

我正在尝试检查图像是否实际上是透明的,而不仅仅是检查 alpha 通道。

为了演示,让我们创建一个具有 Alpha 通道但完全不透明的图像 a.png,以及一个除了一个半透明像素之外相同的图像 b.png

gm convert rose: PNG32:a.png

gm convert rose: -fill '#0008' -draw "matte 10,10 point" PNG32:b.png

使用 ImageMagick,我们可以通过 %[opaque] 轻松检查透明度

$ identify -format '%[opaque]' a.png
true
$ identify -format '%[opaque]' b.png
false

graphicsmagick 的等价物是什么,%A 只检查透明度是否支持而不是图像实际上是透明的。

【问题讨论】:

    标签: image png alpha graphicsmagick


    【解决方案1】:

    更新答案

    后来想到了一种稍微简单的方法。阅读下面的原始答案以了解我在做什么。

    您可以使用 gm 提取 alpha/opacity 通道,然后您不必担心 -verbose 信息输出中的多个通道:

    gm convert b.png -channel opacity -verbose info:-
    

    样本输出

    gm convert a.png -channel opacity -verbose info:- 
    a.png PNG 70x46+0+0 DirectClass 8-bit 7.6Ki 0.000u 0m:0.000000s
    Image: a.png
      Format: PNG (Portable Network Graphics)
      Geometry: 70x46
      Class: DirectClass
      Type: grayscale
      Depth: 1 bits-per-pixel component
      Channel Depths:
        Gray:     1 bits
      Channel Statistics:
        Gray:
          Minimum:                     0.00 (0.0000)
          Maximum:                     0.00 (0.0000)
          Mean:                        0.00 (0.0000)
          Standard Deviation:          0.00 (0.0000)
      Filesize: 0
      Interlace: No
      Orientation: Unknown
      Background Color: white
      Border Color: £DFDFDF
      Matte Color: £BDBDBD
      Page geometry: 70x46+0+0
      Compose: Over
      Dispose: Undefined
      Iterations: 0
      Compression: Zip
      Png:IHDR.color-type-orig: 6
      Png:IHDR.bit-depth-orig: 8
      Signature: d7e8478261a01c7f4c4f6bbb172976d1bd585c1b43195cdb65bafb008f71b5c6
      Tainted: True
    a.png INFO 70x46+0+0 DirectClass 8-bit 0.000u 0m:0.010000s
    

    现在您可以简单地查找 (grep) 并计算 (-c) 包含单词 "Maximum:" 后跟任何非零数字的行。所以你会得到一个零或一 (-m1) 作为输出:

    gm convert b.png -channel opacity -verbose info:- 2>&1 | grep -c -m1 "Maximum:.*[1-9]"
    

    原答案

    嗯,GraphicsMagick 在很多方面都没有 ImageMagick 发达!

    我想到了几个想法。如果你运行:

    gm identify -verbose a.png > a.txt
    gm identify -verbose b.png > b.txt
    

    然后比较它们,你会看到这样的差异:

    因此,您可以查找"Type: true color with transparency",或查看Channel Statistics->Opacity->Maximum 并检查它是否大于零。这有点难以搜索,因为 Opacity: 这个词出现了两次,但您可以像这样使用 awk 进行搜索:

    gm identify -verbose a.png | awk '/Channel Statistics:/{f=1} (f==1)&&/Opacity:/{f=2} (f==2)&&/Maximum:/&&($2>0.00){print "Non-opaque pixel found"}'
    

    所以,我基本上是检查我见过"Channel Statistics:",然后是"Opacity:",然后寻找"Maximum:",然后检查第二列是否超过零。

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 2011-02-03
      • 1970-01-01
      • 2014-03-30
      • 2016-05-26
      • 2013-05-05
      • 1970-01-01
      • 2011-07-31
      • 2020-03-09
      • 1970-01-01
      相关资源
      最近更新 更多