0

I have a 16bit png file, which I am reading using PIL by opening the file in "rb" mode as shown below.

from PIL import Image
value = np.asarray(Image.open(open(filename, "rb")))
print(value)

This prints

[[[128 128   0]
  [128 128   0]
  [128 128   0]
  ...
  [128 128   0]
  [128 128   0]
  [128 128   0]]]

The same file, when I read using opencv with cv2.IMREAD_UNCHAGED, I get different values

import cv2
im = cv2.imread(filename, cv2.IMREAD_UNCHANGED)
print(im)
[[[    0 32768 32768]
  [    0 32768 32768]
  [    0 32768 32768]
  ...
  [    1 32768 32768]
  [    0 32768 32768]
  [    0 32768 32768]]]

My question is why are the values different. PIL seems to treat it as 8-bit png. The issue is more pronounced in the last channel, the mask with only 0s/1s. Reading using PIL gives only zeros, which is wrong.
Is there no way to open this image using PIL? Would prefer not to use cv2 for compatibility reasons

Dan Mašek
  • 17,852
  • 6
  • 57
  • 85
ssuraj
  • 17
  • 5
  • 1
    Take a look at the [following post](https://stackoverflow.com/questions/32622658/read-16-bit-png-image-file-using-python). It looks like PIL doesn't support reading 16 bits RGB PNG image. – Rotem May 17 '23 at 16:32
  • See here https://stackoverflow.com/a/59836908/2836621 and here https://stackoverflow.com/a/49599824/2836621 – Mark Setchell May 17 '23 at 19:08

0 Answers0