1

我正在使用一个需要 4bpp 索引图像的程序,我想用黑色调色板条目填充我的 1bpp 和 2bpp 图像。我将如何使用 Python 来解决这个问题?我看不到使用 PIL 的方法。

4

1 回答 1

1

我对此图像和ImageMagick进行了一些实验,它包含在大多数 Linux 发行版中,可用于 macOS 和 Windows。

首先是原始图像,然后通过抖动减少到 16 色,然后在不抖动的情况下减少到 16 色。

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

使用抖动:

convert paddington.png -colors 16 d.png

没有抖动:

convert paddington.png +dither -colors 16 e.png

然后我查看了ImageMagick生成的图像,它们都有8bpp!所以,我尝试使用“古老的 NetPBM 套件,发现它会创建一个 4bpp 图像,看起来像这样:

convert paddington.png +dither -colors 16 ppm:- | pnmtopng > result.png

如果我在上面运行pngcheck,它在我看来就像一个 4bpp PNG:

pngcheck -vv result.png

输出

File: result.png (4857 bytes)
  chunk IHDR at offset 0x0000c, length 13
    150 x 150 image, 4-bit palette, non-interlaced
  chunk PLTE at offset 0x00025, length 48: 16 palette entries
  chunk IDAT at offset 0x00061, length 4740
    zlib: deflated, 16K window, default compression
    row filters (0 none, 1 sub, 2 up, 3 avg, 4 paeth):
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      (150 out of 150)
  chunk IEND at offset 0x012f1, length 0
No errors detected in result.png (4 chunks, 56.8% compression).

目前,我建议您考虑“全力以赴”使用 ImageMagick 和 NetPBM。

于 2019-11-16T14:42:01.823 回答