If you want to check that a picture is actually transparent... (not only an alpha channel which might be unused)
Use this command:
convert some_pic.png -verbose info:
(yes, there is a :
at the end of the command)
It is quite verbose. Look for the channels list:
(...)
Channel depth:
red: 16-bit
green: 16-bit
blue: 16-bit
Channel statistics:
(...)
In this example, there are three channels, one for each primary color. But non for alpha. So this image is not transparent.
But you can also get this kind of output:
(...)
Channel depth:
red: 16-bit
green: 16-bit
blue: 16-bit
alpha: 1-bit
Channel statistics:
(...)
Here, there is an alpha channel. However, this does no prove that the image is transparent. It just says that it might be. In the outputs of the command, look for the information about alpha channel:
(...)
Alpha:
min: 255 (1)
max: 255 (1)
mean: 255 (1)
standard deviation: 0 (0)
kurtosis: 0
skewness: 0
(...)
In this example, the alpha says that the picture is opaque: min
= max
= 1 (1 = opaque, 0 = transparent). So even if the image has an alpha channel, the user sees an opaque picture.
You can also get this:
(...)
Alpha:
min: 95 (0.372549)
max: 212 (0.831373)
mean: 111.187 (0.436028)
standard deviation: 19.5635 (0.0767196)
kurtosis: 7.52139
skewness: -2.80445
(...)
This time, min
= 0.372549. This means that some pixels are partly transparent. mean
is also low. It seems that a large part of the image uses transparency.
Depending of the type of check you want to achieve (full opacity, "almost opaque", etc.), you should check min
, mean
and maybe standard deviation
if your request is a bit tricky.
Note: you might be tempted to check integer values for min
, mean
and others, as I did in the first place. After all, it is easier to deal with 95
than 0.372549
. If you choose this route, beware the alpha channel depth. If it is 8 bits, then 255 is the maximum and means "opaque". If it is 16 bits, the maximum is now 65535 and 255 means "almost transparent". Better check the floats in parenthesis, which always range from 0 to 1.
If you suspect that a lot of pictures you will process have no alpha channel at all, it might be useful to first run:
identify -format '%[channels]' some_pic.png
If it dumps:
rgba
there is an alpha channel (the a
in the output) and convert
should be used to check min
, etc.. But if there isn't, there is no need to run convert
. Although I didn't benchmarked these two commands, identify
should be much faster than convert
.