#!/usr/bin/perl use strict; use warnings; use Image::Magick; { my @color = ( [ 255, 0, 0 => 'red' ], [ 0, 255, 0 => 'green' ], [ 0, 127, 255 => 'blue' ], [ 255, 0, 255 => 'pink' ], [ 255, 255, 0 => 'yellow' ], [ 255, 127, 0 => 'orange' ], [ 255, 255, 255 => 'white' ], ); my $tolerance = 64; sub get_approximate_color { my( $r, $g, $b ) = map int( ($_+1) / (2**16) * 255 ), @_; for my $c ( @color ) { return $c->[3] if $c->[0]-$tolerance < $r and $c->[0]+$tolerance > $r and $c->[1]-$tolerance < $g and $c->[1]+$tolerance > $g and $c->[2]-$tolerance < $b and $c->[2]+$tolerance > $b; } # for } # get_approximate_color } for my $file ( @ARGV ) { my $img = Image::Magick->new; $img->Read($file); my %col; my( $w, $h ) = ( $img->Get('width'), $img->Get('height') ); for my $y ( 1 .. $h ) { for my $x ( 1 .. $w ) { my $color = get_approximate_color( $img->GetPixel(x=>$x,y=>$y,normalize=>0) ); $col{$color}++ if length $color; } # for } # for my $most = (sort { $col{$b} <=> $col{$a} } keys %col)[0]; print "$file : $most\n"; } # for