为什么 pillow 和 scikit-image 打开 8 位 RGB 图像的方式不同?

Why do pillow and scikit-image open 8 bit RGB images differently?

我正在用 Pillow 和 Scikit-image 打开一个 8 位图像。 Scikit-image 每个像素三个字节,但 Pillow 每个像素一个字节。

我使用以下代码打开了这张图片:

im1 = skimage.io.imread(img_address)
im2 = Image.open(img_address)

结果:

>>>im1[0,0]
array([153, 153, 153], dtype=uint8)
>>>im2.getpixel((0,0))
13

我想知道array([153, 153, 153], dtype=uint8)如何转换为13.

您的 PNG 文件似乎是 Pallete 编码的 PNG 文件。此类文件中的像素由 PNG 类型调色板中的整数值描述,从而减小了文件大小。要以 RGB 格式查看文件,您需要使用 im = im.convert('RGB').

将图像转换为 RGB

如果您随后使用 im.getpixel((0,0)) 请求像素值,您将获得您期望获得的 (153, 153, 153)

总而言之,scikit-image 包会自动进行 PNG 转换,而 pillow returns 则由您提供原始数据并留给您将图像转换为 RGB

有关 [libpng.org][1]

PNG 调色板的更多信息
8.5.1. Palette-Based
Palette-based images, also known as colormapped or index-color images, use the PLTE chunk and are supported in four pixel depths: 1, 2, 4, and 8 bits, corresponding to a maximum of 2, 4, 16, or 256 palette entries. Unlike GIF images, however, fewer than the maximum number of entries may be present. On the other hand, GIF does support pixel depths of 3, 5, 6, and 7 bits; 6-bit (64-color) images, in particular, are common on the World Wide Web.

TIFF also supports palette images, but baseline TIFF allows only 4- and 8-bit pixel depths. Perhaps a more useful comparison is with the superset of baseline TIFF that is supported by Sam Leffler's free libtiff, which has become the software industry's unofficial standard for TIFF decoding. libtiff supports palette bit depths of 1, 2, 4, 8, and 16 bits. Unlike PNG and GIF, however, the TIFF palette always uses 16-bit integers for each red, green, and blue value, and as with GIF, all 2bit depth entries must be present in the file. Nor is there any provision for compression of the palette data--so a 16-bit TIFF palette would require 384 KB all by itself.


  [1]: http://www.libpng.org/pub/png/book/chapter08.html