Page 1 of 1

check if an image has transparency

Posted: 2011-04-21T10:15:04-07:00
by Puic
Hi everyone,

I would like to check if an image as transparency or not in order to convert them to jpeg.

I didn't found much about that and the only answers I got was to check the alpha channel.
That worked but sometimes png files has no transparency and the alpha channel still at 1.

I was wondering if it was a better way to find if an image has transparency or not ?

Thanks you ! ;)

Re: check if an image has transparency

Posted: 2011-04-21T10:38:48-07:00
by fmw42
see the string format %[opaque]. It says if the image is opaque. If true opaque, if false, then the image has some transparency. It is relatively new v6.6.8-9, so you may need an upgrade.

convert image -format "%[opaque]" info:

The alternate is to turn on and extract the alpha channel. Then look at its statistics. If the mean is less than 1, then it has some transparency in it. Otherwise, if mean=1 and std=0, then perfectly opaque.

mean=`convert image -alpha on -alpha extract -format "%[fx:mean]" info:`
std=`convert image -alpha on -alpha extract -format "%[fx:standard_deviation]" info:`
echo "mean=$mean"

see string formats at http://www.imagemagick.org/script/escape.php and http://www.imagemagick.org/script/fx.php