【问题标题】:Convert Color-Indexed Image to Indexes将颜色索引图像转换为索引
【发布时间】:2016-01-25 23:46:40
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: image colors libraries


    【解决方案1】:

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

    #!/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 像素的调色图像:

    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)
    

    请注意,如果您的原始图像只有很少的调色板条目,则输出图像会很暗,因此您需要对它进行对比度拉伸或标准化...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-02
      • 2016-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多