简体   繁体   English

PerlMagick:使用Histogram()输出中的QueryColorname()

[英]PerlMagick: Use QueryColorname() from Histogram() output

I want to get histogram of unique color in an image with color names or their Hex code. 我想在具有颜色名称或其十六进制代码的图像中获得唯一颜色的直方图。

I am unable to convert histogram method output value into color name or Hex code using QueryColorname method; 我无法使用QueryColorname方法将直方图方法的输出值转换为颜色名称或十六进制代码; It always returns black and does not return Hex code. 它始终返回黑色,不返回十六进制代码。

It is possibly due to (0 ... 65535) result range from histogram() method which I am not able to convert into (0 .. 255), an acceptable range for Querycolorname() method. 可能是由于histogram()方法的(0 ... 65535)结果范围,我无法将其转换为Querycolorname()方法的可接受范围(0 .. 255)。

#!/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";
   }
}

Result looks like, 结果看起来像

Red Green Blue Opacity Count Name 红色绿色蓝色不透明度计数名称
0 0 0 0 16134 black 0 0 0 0 16134黑色
257 257 257 0 27 black 257257257257 0 27黑色
0 257 0 0 303 black 0257 00303黑色
257 0 0 0 286 black 257 0 0 0 286黑色
257 257 0 0 8 black 257257 0 0 8黑色
71 0 0 0 82 black 71 0 0 0 82黑色

Method descriptions at http://www.imagemagick.org/script/perl-magick.php http://www.imagemagick.org/script/perl-magick.php上的方法描述

First of all: as you are using single quotes around variables they don't get expanded. 首先:当您在变量周围使用单引号时,它们不会扩展。 QueryColorname sees a string that it possibly converts to zero's. QueryColorname看到一个可能会转换为零的字符串。 That's why all colors are "black". 这就是为什么所有颜色都是“黑色”的原因。

Second: I don't see rgb16 in the docu and I suppose it does not do what you want. 第二:我在文档中没有看到rgb16 ,我想它没有做您想要的。 Instead, you have to scale down to 8 Bit 相反,您必须缩小到8位

Putting both together I propose something like this for the inner if-Block: 将两者放在一起,我为内部的if-Block提出了这样的内容:

my $colVec = "rgb(";
$colVec .= int($histogram[$i-4]/65535*256) . ",";
$colVec .= int($histogram[$i-3]/65535*256) . ",";
$colVec .= int($histogram[$i-2]/65535*256) . ",";
$colVec .= $histogram[$i-1] . ")";
print $image->QueryColorname($colVec) . "\n";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM