我想在具有颜色名称或其十六进制代码的图像中获取唯一颜色的直方图。
我无法使用 QueryColorname 方法将直方图方法的输出值转换为颜色名称或十六进制代码;它总是返回黑色并且不返回十六进制代码。
这可能是由于 histogram() 方法的 (0 ... 65535) 结果范围,我无法将其转换为 (0 .. 255),这是 Querycolorname() 方法的可接受范围。
#!/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";
}
}
结果看起来像,
红色 绿色 蓝色 不透明度 计数 名称
0 0 0 0 16134 黑色
257 257 257 0 27 黑色
0 257 0 0 303 黑色
257 0 0 0 286 黑色
257 257 0 0 8 黑色
71 0 0 0 82 黑色