【问题标题】:PerlMagick: Use QueryColorname() from Histogram() outputPerlMagick:使用 Histogram() 输出中的 QueryColorname()
【发布时间】:2015-02-01 19:24:48
【问题描述】:

我想用颜色名称或其十六进制代码获取图像中唯一颜色的直方图。

我无法使用 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 黑色

http://www.imagemagick.org/script/perl-magick.php的方法说明

【问题讨论】:

    标签: perl imagemagick perlmagick


    【解决方案1】:

    首先:当您在变量周围使用单引号时,它们不会被扩展。 QueryColorname 看到一个可能转换为零的字符串。这就是为什么所有颜色都是“黑色”的原因。

    第二:我在文档中没有看到rgb16,我想它不会做你想要的。相反,您必须缩小到 8 位

    将两者放在一起,我为内部 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";
    

    【讨论】:

      猜你喜欢
      • 2013-07-28
      • 2023-04-10
      • 2011-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-27
      • 1970-01-01
      相关资源
      最近更新 更多