Updated Answer
As pointed out by @ikegami (thank you), GIFs have a designated transparent "colour" pixel in their palette rather than a alpha/transparency layer in which each pixel has its own transparency value, normally between 0-255.
I have generated a 2x2 pixel GIF with one transparent pixel, one red, one green and one blue - then ran ImageMagick's identify
command against it and got the following. I have marked the transparent pixel parts with an arrow.
Image: a.gif
Format: GIF (CompuServe graphics interchange format)
Mime type: image/gif
Class: PseudoClass
Geometry: 4x4+0+0
Units: Undefined
Type: PaletteAlpha
Endianess: Undefined
Colorspace: sRGB
Depth: 8/1-bit
Channel depth:
red: 1-bit
green: 1-bit
blue: 1-bit
alpha: 1-bit
Channel statistics:
Pixels: 16
Red:
min: 0 (0)
max: 255 (1)
mean: 127.5 (0.5)
standard deviation: 127.5 (0.5)
kurtosis: -2
skewness: 0
Green:
min: 0 (0)
max: 255 (1)
mean: 127.5 (0.5)
standard deviation: 127.5 (0.5)
kurtosis: -2
skewness: 0
Blue:
min: 0 (0)
max: 255 (1)
mean: 127.5 (0.5)
standard deviation: 127.5 (0.5)
kurtosis: -2
skewness: 0
Alpha:
min: 0 (0)
max: 255 (1)
mean: 191.25 (0.75)
standard deviation: 110.418 (0.433013)
kurtosis: -0.666667
skewness: 1.1547
Image statistics:
Overall:
min: 0 (0)
max: 255 (1)
mean: 111.562 (0.4375)
standard deviation: 123.451 (0.484123)
kurtosis: -1.8275
skewness: 0.271109
Alpha: srgba(255,255,255,0) #FFFFFF00
Colors: 4
Histogram:
4: ( 0, 0,255,255) #0000FF blue
4: ( 0,255, 0,255) #00FF00 lime
4: (255, 0, 0,255) #FF0000 red
4: (255,255,255, 0) #FFFFFF00 srgba(255,255,255,0)
Colormap entries: 4
Colormap:
0: (255, 0, 0,255) #FF0000 red
1: ( 0,255, 0,255) #00FF00 lime
2: ( 0, 0,255,255) #0000FF blue
3: (255,255,255, 0) #FFFFFF00 srgba(255,255,255,0) <---------
Rendering intent: Perceptual
Gamma: 0.454545
Chromaticity:
red primary: (0.64,0.33)
green primary: (0.3,0.6)
blue primary: (0.15,0.06)
white point: (0.3127,0.329)
Background color: srgba(255,255,255,0)
Border color: srgba(223,223,223,1)
Matte color: grey74
Transparent color: srgba(255,255,255,0) <---------------
Interlace: None
Intensity: Undefined
Compose: Over
Page geometry: 4x4+0+0
Dispose: Undefined
Compression: LZW
Orientation: Undefined
Properties:
date:create: 2014-10-11T10:40:09+01:00
date:modify: 2014-10-11T10:40:08+01:00
signature: 1c82b4c2e772fb075994516cc5661e9dec35b8142f89c651253d07fc3c4642bb
Profiles:
Profile-gif:xmp dataxmp: 1031 bytes
Artifacts:
filename: a.gif
verbose: true
Tainted: False
Filesize: 1.11KB
Number pixels: 16
Pixels per second: 16PB
User time: 0.000u
Elapsed time: 0:01.000
Version: ImageMagick 6.8.9-7 Q16 x86_64 2014-09-10 http://www.imagemagick.org
So, IM does know about the GIF transparent pixel - I will dig some more and see if it can be found sensibly in Perl - for now though, you could just run the following in Perl's backticks.
my $output = `identify -verbose a.gif | grep -i transparent`; # or maybe with FINDSTR on Windows
As an alternatvie, and less problematic across platforms, the %A
escape tells you if an image has transparency enabled:
convert a.gif -print "%A" null:
True
convert a.jpg -print "%A" null:
False
Or, in a more Perl-y way:
#!/usr/bin/perl
use warnings;
use strict;
use Image::Magick;
my $image = Image::Magick->new();
$image->Read($ARGV[0]);
my $a = $image->Get('%A');
print $a;
perl ./script.pl a.gif
True
perl ./script.pl a.jpg
False
Original Answer
I think you may be a little confused about transparency. Images either have transparency, which is an entire layer, or they do not. In general, it is not a question of a single pixel being transparent or not. From the outset, JPEG
s do not support transparency, GIF
and PNG
can support transparency but they are not necessarily always transparent.
So, assuming you have a PNG
or a GIF
, it could have a transparency layer. If it has, each pixel could either be totally transparent, totally opaque or somewhere in between. If you use ImageMagick, it is available at the command line or with PHP, Perl and other bindings.
From the command line, you can tell if an image has a transparency layer using this command:
convert InputImage.png -format "%[opaque]" info:
and it will either return true
or false
.
In Perl you can do this:
#!/usr/bin/perl
use warnings;
use strict;
use Image::Magick;
my $image = Image::Magick->new();
$image->Read($ARGV[0]);
my $a = $image->Get('%[opaque]');
print $a;
then run as:
perl ./script.pl ImageName.png