Pillow
Pillow copied to clipboard
PNG opens as I rather than I;16
What did you do?
Open a 16-bit grayscale png and access its mode
flag.
What did you expect to happen?
im.mode == 'I;16'
What actually happened?
im.mode == 'I'
What are your OS, Python and Pillow versions?
- OS: Ubuntu 18.04.2 LTS (Linux 4.15.0-47)
- Python: 3.6.7
- Pillow: 6.0.0
import numpy as np
from PIL import Image
ar = np.ones((32, 32), dtype=np.uint16)
im = Image.fromarray(ar)
im.save('foo.png')
im = Image.open('foo.png')
assert im.mode == 'I;16'
> file foo.png
foo.png: PNG image data, 32 x 32, 16-bit grayscale, non-interlaced
I encountered this issue as well not very long ago. Is there any workaround in the meantime to "force" reading as I;16
?
Note that this is a duplicate of #3041
I encountered this issue as well not very long ago. Is there any workaround in the meantime to "force" reading as
I;16
?
Because Pillow doesn't scale images during conversion (#3159), you should just be able to convert the image.
from PIL import Image
im = Image.open("Tests/images/16_bit_binary_pgm.png")
assert im.mode == "I"
assert im.getpixel((0, 0)) == 65535
assert im.convert("I;16").getpixel((0, 0)) == 65535
Just wanted to note that this is still an issue as of Pillow 10.1.0
. It can be make unit testing using Pillow harder because you can't assert that the original image was 16-bit if that is part of what you are trying to test.
16-bit grayscale TIFF files are opened as I;16
, but 16-bit grayscale PNG files for some reason aren't.
Individual plugins set the modes that images are opened in. If you're asking why the code is behaving this why, it is because of https://github.com/python-pillow/Pillow/blob/2bd54260b614e3303f0f3b7d9586a011418c1f91/src/PIL/PngImagePlugin.py#L65
I've created PR #7849 to resolve this.
@radarhere I confirmed that 10.3.0 fixed this issue for us. Thanks!