check if an image has transparency

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Puic
Posts: 1
Joined: 2011-04-21T10:08:38-07:00
Authentication code: 8675308

check if an image has transparency

Post by Puic » 2011-04-21T10:15:04-07:00

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 ! ;)

User avatar
fmw42
Posts: 25144
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: check if an image has transparency

Post by fmw42 » 2011-04-21T10:38:48-07:00

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

Post Reply