将颜色索引图像转换为索引

问题描述 投票:0回答:1

我有一个png格式的图像目录,它使用固定的颜色索引进行编码。我有兴趣生成灰度值对应于索引本身的图像,而不是该索引的颜色。

例如,假设颜色索引具有条目索引3 - >(255,0,0)(红色)。我想用RGB灰度值替换RGB图像中的每个(255,0,0)实例。

我的第一个想法是1)将反转颜色索引硬编码到查找表中2)加载图像,3)迭代像素,基于查找表进行替换。

这个问题是对查找表进行硬编码。我可以得到它(通过imagemagick识别),但这很乏味。有没有可以为我做这个的图书馆?我正在寻找1)cmd线转换或2)获取像素颜色索引的代码库。

image colors libraries
1个回答
1
投票

如果你使用GD在PHP中打开一个palettised图像,但不小心忘记告诉GD它是palettised,你实际上会得到蓝色像素中的调色板索引。因此,您可以利用此功能创建灰度图像,方法是创建与原始图像大小相同的真彩色图像,然后将调色板索引编写三次,每次为红色,绿色和蓝色通道编写一次。它比听起来容易,这里是代码:

#!/usr/local/bin/php
<?php
// Read in a palettised image
$im = imagecreatefrompng("pal.png");

// Create output image same size
$out = imagecreatetruecolor(imagesx($im), imagesy($im));

for ($x = 0; $x < imagesx($im); $x++) {
    for ($y = 0; $y < imagesy($im); $y++) {
        $pixel = imagecolorat($im, $x, $y);
        $index = $pixel & 0xFF;
        $outCol = imagecolorallocate($out,$index,$index,$index);
        imagesetpixel($out,$x,$y,$outCol);
        // printf("[%d,%d] Palette index:%d\n",$x,$y,$index);
    }
}
imagepng($out,"result.png");
?>

所以,如果我使用ImageMagick创建一个3像素的palettised图像,如下所示:

convert xc:red xc:lime xc:blue +append pal.png

并检查它有这样的调色板

    identify -verbose pal.png  | more
    Image: pal.png
      Format: PNG (Portable Network Graphics)
      Mime type: image/png
      Class: PseudoClass
      Geometry: 3x1+0+0
      Units: Undefined
      Type: Palette                  <--- it has a palette
      Endianess: Undefined
      Colorspace: sRGB
      Depth: 8/1-bit
      Channel depth:
        red: 1-bit
        green: 1-bit
        blue: 1-bit
      Channel statistics:
        Pixels: 3
        Red:
          min: 0 (0)
          max: 255 (1)
          mean: 85 (0.333333)
          standard deviation: 120.208 (0.471405)
          kurtosis: -1.5
          skewness: 0.707107
          entropy: 0.918296
        Green:
          min: 0 (0)
          max: 255 (1)
          mean: 85 (0.333333)
          standard deviation: 120.208 (0.471405)
          kurtosis: -1.5
          skewness: 0.707107
          entropy: 0.918296
        Blue:
          min: 0 (0)
          max: 255 (1)
          mean: 85 (0.333333)
          standard deviation: 120.208 (0.471405)
          kurtosis: -1.5
          skewness: 0.707107
          entropy: 0.918296
      Image statistics:
        Overall:
          min: 0 (0)
          max: 255 (1)
          mean: 85 (0.333333)
          standard deviation: 120.208 (0.471405)
          kurtosis: -1.5
          skewness: 0.707107
          entropy: 0.918296
      Colors: 3
      Histogram:
             1: (  0,  0,255) #0000FF blue
             1: (  0,255,  0) #00FF00 lime
             1: (255,  0,  0) #FF0000 red
      Colormap entries: 4
      Colormap:                                       <--- here is the palette
             0: (255,  0,  0) #FF0000 red
             1: (  0,255,  0) #00FF00 lime
             2: (  0,  0,255) #0000FF blue
             3: (255,255,255) #FFFFFF white

然后在运行PHP脚本后检查result.png,它现在看起来像这样 - 即灰度和颜色匹配前面的索引。

identify -verbose result.png
Image: result.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: DirectClass
  Geometry: 3x1+0+0
  Units: Undefined
  Type: Grayscale               <--- it is now greyscale, no palette
  Endianess: Undefined
  Colorspace: sRGB
  Depth: 8-bit
  Channel depth:
    gray: 8-bit
  Channel statistics:
    Pixels: 3
    Gray:
      min: 0 (0)
      max: 2 (0.00784314)
      mean: 1 (0.00392157)
      standard deviation: 0.816497 (0.00320195)
      kurtosis: -1.5
      skewness: 0
      entropy: 1
  Colors: 3
  Histogram:
         1: (  0,  0,  0) #000000 gray(0)
         1: (  1,  1,  1) #010101 gray(1)
         1: (  2,  2,  2) #020202 gray(2)

请注意,如果原始图像的调色板条目非常少,则输出图像会变暗,因此您需要对比拉伸或标准化...

© www.soinside.com 2019 - 2024. All rights reserved.