How to detect if image (gif) is fully transparent?

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
PSS
Posts: 4
Joined: 2014-04-30T12:35:00-07:00
Authentication code: 6789

How to detect if image (gif) is fully transparent?

Post by PSS »

I need Imagemagick to tell me if an image (mostly gif) is rendered on a browser as fully transparent. How is that done with Imagemagick?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to detect if image (gif) is fully transparent?

Post by fmw42 »

What is your IM version and platform?

The best way I know is to test if the alpha channel has a mean=0. A fully transparent image has an alpha channel that is totally black.

Code: Select all

convert image -channel a -format "%[mean]" info:
see
http://www.imagemagick.org/script/escape.php
PSS
Posts: 4
Joined: 2014-04-30T12:35:00-07:00
Authentication code: 6789

Re: How to detect if image (gif) is fully transparent?

Post by PSS »

Thanks a lot, seems to work fine. :)
What I get is a value of 255 when the image is fully transparent.

I use 6.8.3-8 2013-03-04 Q8 in Windows, and usually the latest version on Linux.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to detect if image (gif) is fully transparent?

Post by fmw42 »

PSS wrote:Thanks a lot, seems to work fine. :)
What I get is a value of 255 when the image is fully transparent.

I use 6.8.3-8 2013-03-04 Q8 in Windows, and usually the latest version on Linux.
It looks like it is reporting the opacity and not the transparency and that may be what IM 6 does. I will report it and see what they say.

in the mean time, just do

Code: Select all

convert image -channel a -negate -format "%{mean]" info:
or just see if you get exactly 255.

Alternately, this works fine

Code: Select all

convert image -channel a -separate -scale 1x1! -format "%[fx:u]\n" info:
it reports 0 for fully transparent and 1 for fully opaque
PSS
Posts: 4
Joined: 2014-04-30T12:35:00-07:00
Authentication code: 6789

Re: How to detect if image (gif) is fully transparent?

Post by PSS »

(there seems to be a bracket typo, should be convert image -channel a -negate -format "%[mean]" info:)
It looks like it is reporting the opacity and not the transparency and that may be what IM 6 does. I will report it and see what they say.
So you say the first command might not give correct results (255 == transparent) when IM versions get updated, or between platforms?

Re: convert image -channel a -separate -scale 1x1! -format "%[fx:u]" info:
Drawback on this is it returns same value for all of the frames when gif is animated. With two other commands I can get each frame separately - not a big deal but worth pointing out.

Thanks!

Example gif (Googled, no mine :)) : http://oakley-graphics.wikispaces.com/f ... mation.gif gets you

convert S:/gif/zara-cartoon-animation.gif -channel a -format "%[mean]" info:

0=> 200.538
1=> 200.538
2=> 200.538
3=> 200.538
4=> 200.538
5=> 200.538
6=> 200.297
7=> 196.024
8=> 196.443
9=> 199.903
10=> 202.089
11=> 0

convert S:/gif/zara-cartoon-animation.gif -channel a -negate -format "%[mean]" info:

0=> 54.4617
1=> 54.4617
2=> 54.4621
3=> 54.4618
4=> 54.4618
5=> 54.4618
6=> 54.7029
7=> 58.9764
8=> 58.5568
9=> 55.0973
10=> 52.9107
11=> 255

convert S:/gif/zara-cartoon-animation.gif -channel a -separate -scale 1x1! -format "%[fx:u]" info:

0=> 0.211765
1=> 0.211765
2=> 0.211765
3=> 0.211765
4=> 0.211765
5=> 0.211765
6=> 0.211765
7=> 0.211765
8=> 0.211765
9=> 0.211765
10=> 0.211765
11=> 0.211765
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to detect if image (gif) is fully transparent?

Post by fmw42 »

The second one should be fine.

Recall the first two report in the range 0 to 255, while the last reports in the range 0 to 1. The first should give results that (255 - the results from the second) as that is reporting opacity. The second and third are reporting transparency.

You never mentioned using animated gifs. It is always best to be clear about what you are doing so we do not make assumptions.

If you want to post your animation to some free service such as dropbox.com (public folder) and put a link here we can test and confirm.

Since you have an animated gif, try this variation of the third one.

Code: Select all

convert image.gif -coalesce -channel a -separate -scale 1x1! -format "%[fx:u]\n" info:
PSS
Posts: 4
Joined: 2014-04-30T12:35:00-07:00
Authentication code: 6789

Re: How to detect if image (gif) is fully transparent?

Post by PSS »

Animation is part of gif standard is it :)

I need to check any kind of gif (and png). I can not know in advance if they are animated or not, but that is not a problem as long as the result values are "solid". I use PHP exec to run convert and get an array as result. From it is is easy to determine for example if animated gif ends in fully transparent frame. If result array has only one key it is not animated.

So I think I get what I need now.

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

Re: How to detect if image (gif) is fully transparent?

Post by fmw42 »

adding -coalesce to command should not cause problems if the gif is not animated.
Post Reply