Page 1 of 1

Use QueryColorname() from Histogram() output

Posted: 2014-12-03T13:14:56-07:00
by usyed
I want to get histogram with color names or their Hex code.

I am unable to convert histogram output value into color name or Hex code using QueryColorname; It always returns black and does not return Hex code. Please help in this step.

Code: Select all

#!/usr/bin/perl
use Image::Magick;

$image=Image::Magick->new();
$image->ReadImage('Sun.jpeg'); 

my @histogram = $image->Histogram();
print "Red\tGreen\tBlue\tOpacity\tCount\tName\n";
for(my $i=0; $i<=29; $i++){ #Get 5 unique colors
	print "$histogram[$i]\t";
	if (($i+1)%5 == 0){ #Array elements of unique color
		my $name = $image->QueryColorname('rgb16($histogram[$i-4],$histogram[$i-3],$histogram[$i-2],$histogram[$i-1])');
		print "$name\n";
	}
}
  • Red Green Blue Opacity Count Name
    0 0 0 0 16134 black
    257 257 257 0 27 black
    0 257 0 0 303 black
    257 0 0 0 286 black
    257 257 0 0 8 black
    771 0 0 0 82 black