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

Conversion P to L wrong? #5728

Closed
CTPaHHuK-HEbA opened this issue Sep 21, 2021 · 2 comments · Fixed by #5824
Closed

Conversion P to L wrong? #5728

CTPaHHuK-HEbA opened this issue Sep 21, 2021 · 2 comments · Fixed by #5824

Comments

@CTPaHHuK-HEbA
Copy link

CTPaHHuK-HEbA commented Sep 21, 2021

from PIL import Image

img = Image.open('p.png')
print('P->L: ', img.convert('L').getpixel((0, 0)))
print('P->RGB: ', img.convert('RGB').getpixel((0, 0)))
print('P->RGB->L: ', img.convert('RGB').convert('L').getpixel((0, 0)))

p.png: p

Old
PIL: 6.2.2
P->L: 175
P->RGB: (177, 175, 175)
P->RGB->L: 175

7.0.0+
After commit #4320
PIL: 8.3.2
P->L: 175
P->RGB: (177, 175, 175)
P->RGB->L: 176

@radarhere radarhere changed the title Conversion P to L wrong ? Conversion P to L wrong? Sep 22, 2021
@radarhere
Copy link
Member

radarhere commented Nov 8, 2021

This is because P->L uses the L macro, but RGB->L uses the L24 macro.

So this change would make P->L be 176 as well.

diff --git a/src/libImaging/Convert.c b/src/libImaging/Convert.c
index 9012cfcd7..cd6e536cb 100644
--- a/src/libImaging/Convert.c
+++ b/src/libImaging/Convert.c
@@ -1013,7 +1013,7 @@ p2l(UINT8 *out, const UINT8 *in, int xsize, const UINT8 *palette) {
     int x;
     /* FIXME: precalculate greyscale palette? */
     for (x = 0; x < xsize; x++) {
-        *out++ = L(&palette[in[x] * 4]) / 1000;
+        *out++ = L24(&palette[in[x] * 4]) >> 16;
     }
 }

But I'm by no means certain that this is the correct solution.

@radarhere
Copy link
Member

I've created #5824 to fix this by applying rounding to palette conversion methods.

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

Successfully merging a pull request may close this issue.

2 participants