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

Pillow doesn't properly support 8-bit grayscale PNG #61

Open
illwieckz opened this issue Jan 29, 2023 · 1 comment
Open

Pillow doesn't properly support 8-bit grayscale PNG #61

illwieckz opened this issue Jan 29, 2023 · 1 comment

Comments

@illwieckz
Copy link
Member

illwieckz commented Jan 29, 2023

When loading 8-bit grayscale PNGs with Pillow, and converting them to RGB, they are converted like if they were 1-bit PNGs.

So basically, white,lightgrey,midgrey,darkgrey,black becomes white,white,white,white,black.

For now I'm implementing a workaround by converting all PNGs to lossless WebP as a transient format because cwebp is known to properly convert PNG to lossless WebP before loading the bitmap with Pillow.

For example I do PNG to WebP to TGA to convert from PNG to TGA.

This becomes crazy because we already did PNG to TGA to CRN to convert from PNG to CRN because crunch doesn't read all PNG formats. So, since we cannot do PNG to TGA in one go, I now do PNG to WebP to TGA to CRN to convert from PNG to CRN. This is really crazy!

Some code may now be highly unoptimized like PNG to WebP since the sanitization of the image is converted first to WebP before being converted to WebP again.

I plan to introduce a specific ImageConverter module to handle image conversion properly with the “shortest” (ahem) path for known formats.

This workaround is good enough to build Unvanquished 0.54 release.


Note: in the past we used ImageMagick convert tool to convert from anything to TGA but then ImageMagick broke TGA conversion (both conversion from TGA and to TGA) and maintainer is considering there is no bug, so this other tool is not an option anymore.

Something is wrong with PNG…

@illwieckz illwieckz changed the title Pillow doesn't support 8-bit grayscale PNG properly Pillow doesn't properly support 8-bit grayscale PNG Jan 29, 2023
@illwieckz
Copy link
Member Author

Images to reproduce the bug, from res-buildables_src.dpkdir/models/buildables/medistat:

h_grid.png
noise.png
noise2.png
v_grid.png

Python command to reproduce the bug:

From PIL import Image
Image.open("h_grid.png").convert("RGB").save("h_grid.tga")

illwieckz added a commit that referenced this issue Jan 29, 2023
See #61

When loading 8-bit grayscale PNGs with Pillow, and converting them to RGB,
they are converted like if they were 1-bit PNGs.

So basically, white,lightgrey,midgrey,darkgrey,black
becomes white,white,white,white,black.

For now I'm implementing a workaround by converting all PNGs
to lossless WebP as a transient format because cwebp is known
to properly convert PNG to lossless WebP before loading the
bitmap with Pillow.

For example I do PNG to WebP to TGA to convert from PNG to TGA.

This becomes crazy because we already did PNG to TGA to CRN to convert
from PNG to CRN because crunch doesn't read all PNG formats. So, since
we cannot do PNG to TGA in one go, I now do PNG to WebP to TGA to CRN
to convert from PNG to CRN. This is really crazy!

Some code may now be highly unoptimized like PNG to WebP since the
sanitization of the image is converted first to WebP before being
converted to WebP again.

I plan to introduce a specific ImageConverter module to handle
image conversion properly with the “shortest” (ahem) path
for known formats.

This workaround is good enough to build Unvanquished 0.54 release.
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

No branches or pull requests

1 participant