Skip to content

Grayscale PNG image incorrectly converted to RGB #2574

Description

@nomarek

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

Activity

  1. wiredfool commented on Jun 12, 2017

    @wiredfool
    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')
    
  2. aclark4life commented on Jun 21, 2017

    @aclark4life
    Member

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

  3. keck-in-space commented on Oct 30, 2018

    @keck-in-space

    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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions