Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PNG opens as I rather than I;16 #3796

Closed
kevinptt0323 opened this issue Apr 17, 2019 · 8 comments · Fixed by #7849
Closed

PNG opens as I rather than I;16 #3796

kevinptt0323 opened this issue Apr 17, 2019 · 8 comments · Fixed by #7849
Labels
Bug Any unexpected behavior, until confirmed feature.
Projects

Comments

@kevinptt0323
Copy link

kevinptt0323 commented Apr 17, 2019

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
@aclark4life aclark4life added the Bug Any unexpected behavior, until confirmed feature. label May 11, 2019
@aclark4life aclark4life added this to Backlog in Pillow May 11, 2019
@aclark4life aclark4life moved this from Backlog to In progress in Pillow May 11, 2019
@abusque
Copy link

abusque commented Jul 4, 2019

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

@radarhere radarhere changed the title Open a 16-bit grayscale png with incorrect mode flag PNG opens as I rather than I;16 Aug 19, 2019
@radarhere
Copy link
Member

Note that this is a duplicate of #3041

@radarhere
Copy link
Member

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

@johnthagen
Copy link
Contributor

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.

@g-regor
Copy link

g-regor commented Mar 1, 2024

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

@radarhere
Copy link
Member

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

(16, 0): ("I", "I;16B"),

@radarhere
Copy link
Member

I've created PR #7849 to resolve this.

Pillow automation moved this from In progress to Closed Mar 2, 2024
@johnthagen
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Any unexpected behavior, until confirmed feature.
Projects
Pillow
  
Closed
Development

Successfully merging a pull request may close this issue.

6 participants