user2650277
user2650277

Reputation: 6729

Detect if an image is transparent in GraphicsMagick

I am trying to check whether an image is actually transparent , not just check alpha channel.

To demonstrate, lets create an image a.png that has an alpha channel but is fully opaque, and an image b.png that is the same except for one translucent pixel:

gm convert rose: PNG32:a.png

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

With ImageMagick we can easily check the trasparency with %[opaque]

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

What is the equivalent for graphicsmagick , %A only check if transparency is supported not that the image is actually transparent.

Upvotes: 0

Views: 1601

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207335

Updated Answer

A slightly simpler method has since sprung to mind. Read the original answer below to understand what I am doing.

You can extract the alpha/opacity channel using gm and then you will not have to worry about multiple channels in the -verbose info output:

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

Sample Output

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

Now you can simply look for (grep), and count (-c) the lines that contain the word "Maximum:" followed by any digits other than zero. So you will get a zero or a one (-m1) as output:

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

Original Answer

Mmmm, GraphicsMagick is somewhat less developed than ImageMagick in many respects!

A couple of ideas come to mind. If you run:

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

and diff them, you will see the differences like this:

enter image description here

So, you could either look for "Type: true color with transparency", or look at the Channel Statistics->Opacity->Maximum and check if it is greater than zero. That is somewhat hard to search for because the word Opacity: occurs twice but you can do it with awk like this:

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"}'

So, I am basically checking I have seen "Channel Statistics:", followed by "Opacity:" and then looking for "Maximum:" and then checking if the second column exceeds zero.

Hope that helps!

Upvotes: 1

Related Questions