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

孤者浪人 提交于 2019-12-11 08:29:32

问题


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


回答1:


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?).



来源:https://stackoverflow.com/questions/19892919/pil-converting-an-image-with-mode-i-to-rgb-results-in-a-fully-white-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!