This script ( from author unknown) works on a gif I tested.
#!/usr/bin/perl
#None of these is fast, mainly because of the overhead of calling Perl
#code for each pixel.
use strict;
my $imagefile = shift or die "No file specified\n";
sub rgba2hex {
sprintf "%02x%02x%02x%02x", map { $_ || 0 } @_;
}
{
use Imager;
my %colors;
my $img = Imager->new();
$img->open( file => $imagefile ) or die $img->errstr;
my ( $w, $h ) = ( $img->getwidth, $img->getheight );
for my $i ( 0 .. $w - 1 ) {
for my $j ( 0 .. $h - 1 ) {
my $color = $img->getpixel( x => $i, y => $j );
my $hcolor = rgba2hex $color->rgba();
print "$hcolor ";
$colors{$hcolor}++;
}
}
printf "Imager: Number of colours: %d\n", scalar keys %colors;
}
{
use GD;
my %colors;
my $gd = GD::Image->new($imagefile) or die "GD::Image->new($imagef
+ile)";
my ( $w, $h ) = $gd->getBounds();
for my $i ( 0 .. $w - 1 ) {
for my $j ( 0 .. $h - 1 ) {
my $index = $gd->getPixel( $i, $j );
my $hcolor = rgba2hex( $gd->rgb($index), 0 );
$colors{$hcolor}++;
}
}
printf "GD: Number of colours: %d\n", scalar keys %colors;
}
{
use Image::Magick;
my %colors;
my $img = Image::Magick->new();
my $rc = $img->Read($imagefile);
die $rc if $rc;
my ( $w, $h ) = $img->Get( 'width', 'height' );
for my $i ( 0 .. $w - 1 ) {
for my $j ( 0 .. $h - 1 ) {
my $color = $img->Get("pixel[$i,$j]");
my $hcolor = rgba2hex split /,/, $color;
$colors{$hcolor}++;
}
}
printf "Image::Magick: Number of colours: %d\n", scalar keys %colo
+rs;
}
I'm not really a human, but I play one on earth.
flash japh
|