Reputation: 333
I'm opening an 8-bit image with both Pillow and Scikit-image. Scikit-image gives three bytes per pixel, but Pillow gives one byte per pixel.
I've used the following code to open this image:
im1 = skimage.io.imread(img_address)
im2 = Image.open(img_address)
results:
>>>im1[0,0]
array([153, 153, 153], dtype=uint8)
>>>im2.getpixel((0,0))
13
I want to know how array([153, 153, 153], dtype=uint8) is converted to 13.
Upvotes: 1
Views: 1388
Reputation: 3118
Your PNG file seems to be a Pallete encoded PNG file. A pixel in such a file is described by an integer value in a PNG type palette, thus reducing the file size. To view your file in RGB you need to convert the image to RGB with im = im.convert('RGB')
.
If you then request the pixel value with im.getpixel((0,0))
you will get the (153, 153, 153)
that you expect to get.
To summarise, scikit-image package does the PNG conversion automatically, while pillow returns you the raw data and leaves it to you to convert the image to RGB
More on the PNG palette from [libpng.org][1]
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
Upvotes: 4