Salvatore Iovene
Salvatore Iovene

Reputation: 2323

PIL: converting an image with mode "I" to "RGB" results in a fully white image

The image at the end of this question is a PNG with mode I, which stands for Indexed, as far as I can tell.

I'm trying to create a thumbnail out of it, and save it as JPG with PIL.

However, is I leave the mode alone, PIL won't let me resize it with error unable to generate thumbnail: cannot write mode I as JPEG.

If I convert it to RGB, the result will be a fully white image.

Is there a way to fix this?

https://www.dropbox.com/s/2d1edk2iu4ixk25/NGC281.png

Upvotes: 2

Views: 2302

Answers (1)

Jongware
Jongware

Reputation: 22478

The input image is a 16-bit grayscale PNG, and it appears PIL has a problem with this. Manually converting it to an 8-bit image before further processing makes it work again.

The problem may originate inside PIL itself. The PyPNG homepage asserts

..PIL only has internal representations (PIL mode) for 1-bit and 8-bit channel values. This makes me wonder if PIL can read PNG files with bit depth 2 or 4 (greyscale or palette), and also bit depth 16 (which PNG supports for greyscale and RGB images).

Then again, that page is from 2009. It could be worth tracking down where PIL is maintained from, and report this as a bug (? Or possibly a feature request?).

Upvotes: 1

Related Questions