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

PGM and PPM maximum grey/color value is ignored #5008

Closed
gofr opened this issue Oct 26, 2020 · 1 comment · Fixed by #6119
Closed

PGM and PPM maximum grey/color value is ignored #5008

gofr opened this issue Oct 26, 2020 · 1 comment · Fixed by #6119

Comments

@gofr
Copy link
Contributor

gofr commented Oct 26, 2020

What did you do?

from PIL import Image


header = b"""\
P5
15 7
11
"""
data = [
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,   0,  0,  0,  0,  0,
    0,  3,  3,  3,  3,  0,  7,  7,  7,  0,  11,  0,  0,  0,  0,
    0,  3,  0,  0,  3,  0,  0,  7,  0,  0,  11,  0,  0,  0,  0,
    0,  3,  3,  3,  3,  0,  0,  7,  0,  0,  11,  0,  0,  0,  0,
    0,  3,  0,  0,  0,  0,  0,  7,  0,  0,  11,  0,  0,  0,  0,
    0,  3,  0,  0,  0,  0,  7,  7,  7,  0,  11, 11, 11, 11,  0,
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,   0,  0,  0,  0,  0
]
with open('pil.pgm', 'wb') as fp:
    fp.write(header)
    fp.write(bytes(data))

img = Image.open('pil.pgm')
img.show()
img.save('pil.png')

What did you expect to happen?

Both the image preview and saved PNG show a 15x7 greyscale image with the letters "PIL", where the "P" is dark grey, the "I" light grey and the "L" is white. This is what I see if I load the "pil.pgm" in other software, such as Ubuntu's file manager, image viewer, ImageMagick and GIMP.

What actually happened?

The PIL letters are all really dark grey.

Pillow only uses the "11" maxval to determine how many bytes to use for each pixel. E.g. a value of 10000 makes it use 2 bytes per pixel. It does not use it as the maximum value. It uses 255 as the maximum instead.

Similarly, with 16-bit grey values, the maximum is always 65535.
The same happens for the RGB P6 format. The maxval is only used to determine the bit depth.

What are your OS, Python and Pillow versions?

  • OS: Ubuntu 20.04
  • Python: 3.8.5
  • Pillow: built from 8dfc036
@radarhere
Copy link
Member

radarhere commented Mar 9, 2022

I've created PR #6119 to resolve this.

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

Successfully merging a pull request may close this issue.

2 participants