Pillow icon indicating copy to clipboard operation
Pillow copied to clipboard

PNG opens as I rather than I;16

Open kevinptt0323 opened this issue 5 years ago • 4 comments

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

kevinptt0323 avatar Apr 17 '19 09:04 kevinptt0323

I encountered this issue as well not very long ago. Is there any workaround in the meantime to "force" reading as I;16?

abusque avatar Jul 04 '19 19:07 abusque

Note that this is a duplicate of #3041

radarhere avatar Sep 04 '19 20:09 radarhere

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

radarhere avatar Sep 04 '23 01:09 radarhere

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.

johnthagen avatar Nov 15 '23 13:11 johnthagen

16-bit grayscale TIFF files are opened as I;16, but 16-bit grayscale PNG files for some reason aren't.

g-regor avatar Mar 01 '24 20:03 g-regor

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

radarhere avatar Mar 02 '24 02:03 radarhere

I've created PR #7849 to resolve this.

radarhere avatar Mar 02 '24 04:03 radarhere

@radarhere I confirmed that 10.3.0 fixed this issue for us. Thanks!

johnthagen avatar Apr 02 '24 13:04 johnthagen