How to detect if image (gif) is fully transparent?
How to detect if image (gif) is fully transparent?
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?
- 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?
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.
see
http://www.imagemagick.org/script/escape.php
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:
http://www.imagemagick.org/script/escape.php
Re: How to detect if image (gif) is fully transparent?
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.

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.
- 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?
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.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.
in the mean time, just do
Code: Select all
convert image -channel a -negate -format "%{mean]" info:
Alternately, this works fine
Code: Select all
convert image -channel a -separate -scale 1x1! -format "%[fx:u]\n" info:
Re: How to detect if image (gif) is fully transparent?
(there seems to be a bracket typo, should be convert image -channel a -negate -format "%[mean]" info:)
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
So you say the first command might not give correct results (255 == transparent) when IM versions get updated, or between platforms?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.
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

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
- 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?
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.
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:
Re: How to detect if image (gif) is fully transparent?
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.

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.
- 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?
adding -coalesce to command should not cause problems if the gif is not animated.