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

Grayscale PNG image incorrectly converted to RGB #2574

Closed
nomarek opened this issue Jun 12, 2017 · 3 comments
Closed

Grayscale PNG image incorrectly converted to RGB #2574

nomarek opened this issue Jun 12, 2017 · 3 comments

Comments

@nomarek
Copy link

nomarek commented Jun 12, 2017

I have a PNG image which incorrectly converts to RGB mode. This is a grayscale 16-bit image, presumably created in Photoshop. When I open it with Pillow, mode of an image is described as "I". I tried to convert it by calling:

from PIL import Image
Image.open('original.png').convert('RGB').save('result.png')

All I received was a white image with only black pixels rgb(0, 0, 0) copied from the original. I am using newest version of Pillow (4.1.1), problem appears both in Python 2.7 & 3.6.
I am attaching 2 files:

  • original.png - original image used to reproduce this problem
    original
  • result.png - result of the conversion
    result
@wiredfool
Copy link
Member

So what's happening here is that the convert from 'I' to 'L' loses information, and the conversion isn't done is a particularly useful fashion. It's clamping the range of the 16bit image to 0-255, rather than (auto)scaling it.

Unfortunately, I don't see a quick way to autorange the image so that it fits into the destination image format, but there's a workaround with imagemath. (any of the embedded functions that rely on histogram or lookup tables aren't going to work)

>>> from PIL import ImageMath
>>> im2 = ImageMath.eval('im/256', {'im':im}).convert('L')

@aclark4life
Copy link
Member

I'm going to call this "documented workaround" and close.

@keck-in-space
Copy link

This should be re-opened and fixed IMO. Or at least better documented. Digging into closed issues isn't intuitive. It took quite a while to find this fix.

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

No branches or pull requests

4 participants