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