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

10 bit pgm file not read correctly #5403

Closed
jesmanson opened this issue Apr 13, 2021 · 8 comments · Fixed by #6119
Closed

10 bit pgm file not read correctly #5403

jesmanson opened this issue Apr 13, 2021 · 8 comments · Fixed by #6119

Comments

@jesmanson
Copy link

jesmanson commented Apr 13, 2021

What did you do?

Load 10 bit pgm image and saved it as png image

What did you expect to happen?

Pixel values scaled to use the 16 bit range
(http://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html)

What actually happened?

pixel values stored in the least significant bytes and hence the image is too dark

What are your OS, Python and Pillow versions?

  • OS: Ubuntu 18.04.5 LTS
  • Python: 3.6
  • Pillow: 8.2.0
from PIL import Image

input_path_pgm = 'path-to-10-bit-pgm-image'
output_path_png = 'path-to-16-bit-png-image'
image = Image.open(input_path_pgm)
image.save(output_path_png, format='PNG')
@radarhere
Copy link
Member

Could you provide a copy of the input image?

@jesmanson
Copy link
Author

Yeah sure! Here is an example of a 10 bit pgm image.
cat.zip

@radarhere
Copy link
Member

Just for the record, the rawmode of this image is detected as I;16B

@wiredfool
Copy link
Member

Pillow does not auto-scale image values, even when converting between 8 bit and 16 bit formats.

This is a longstanding issue with expectations, but changing it at this point would be a breaking change.

@morincl
Copy link

morincl commented May 21, 2021

Considering that this unexpected behavior is undocumented (correct me if I am wrong) and violates the PNG specification, such a breaking change would maybe be acceptable?

@radarhere
Copy link
Member

radarhere commented Mar 2, 2022

I've created PR #6107 to resolve this.

Just to be clear - there is no problem with how the PNG is saved in the original post here. The problem is just in how a 10-bit PGM file is read by Pillow.

@radarhere radarhere changed the title 10 bit pgm file not scaled correctly when saved as png file 10 bit pgm file not read correctly Mar 2, 2022
@kmilos
Copy link
Contributor

kmilos commented Mar 3, 2022

Why stop at 10-bit? There are 12-bit pixel formats as well, e.g. used in very high quality HDR production. FFmpeg also defines 14-bit, and even 9-bit... 12 and 14 are also common bit depths for camera sensors, and raw data is sometimes dumped/converted to PGM(-like) files.

@radarhere
Copy link
Member

Ok, I've added 9, 12 and 14 to the PR as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants