diff --git a/Tests/images/palette_negative.png b/Tests/images/palette_negative.png index 938a7285fd7..7fcfd29a0db 100644 Binary files a/Tests/images/palette_negative.png and b/Tests/images/palette_negative.png differ diff --git a/Tests/images/palette_sepia.png b/Tests/images/palette_sepia.png index f3fc932531f..9e7d6b0345b 100644 Binary files a/Tests/images/palette_sepia.png and b/Tests/images/palette_sepia.png differ diff --git a/Tests/images/palette_wedge.png b/Tests/images/palette_wedge.png index 23fb7940d6d..4b3d9ff3a21 100644 Binary files a/Tests/images/palette_wedge.png and b/Tests/images/palette_wedge.png differ diff --git a/Tests/test_image_entropy.py b/Tests/test_image_entropy.py index 876d676fe39..ea5886e72db 100644 --- a/Tests/test_image_entropy.py +++ b/Tests/test_image_entropy.py @@ -9,7 +9,7 @@ def entropy(mode): assert round(abs(entropy("L") - 7.063008716585465), 7) == 0 assert round(abs(entropy("I") - 7.063008716585465), 7) == 0 assert round(abs(entropy("F") - 7.063008716585465), 7) == 0 - assert round(abs(entropy("P") - 5.0530452472519745), 7) == 0 + assert round(abs(entropy("P") - 5.082506854662517), 7) == 0 assert round(abs(entropy("RGB") - 8.821286587714319), 7) == 0 assert round(abs(entropy("RGBA") - 7.42724306524488), 7) == 0 assert round(abs(entropy("CMYK") - 7.4272430652448795), 7) == 0 diff --git a/Tests/test_image_getcolors.py b/Tests/test_image_getcolors.py index e5b6a772462..7fd0398f947 100644 --- a/Tests/test_image_getcolors.py +++ b/Tests/test_image_getcolors.py @@ -16,7 +16,7 @@ def getcolors(mode, limit=None): assert getcolors("L") == 255 assert getcolors("I") == 255 assert getcolors("F") == 255 - assert getcolors("P") == 90 # fixed palette + assert getcolors("P") == 96 # fixed palette assert getcolors("RGB") is None assert getcolors("RGBA") is None assert getcolors("CMYK") is None diff --git a/Tests/test_image_histogram.py b/Tests/test_image_histogram.py index 91e02973d04..0ee52e724e1 100644 --- a/Tests/test_image_histogram.py +++ b/Tests/test_image_histogram.py @@ -10,7 +10,7 @@ def histogram(mode): assert histogram("L") == (256, 0, 662) assert histogram("I") == (256, 0, 662) assert histogram("F") == (256, 0, 662) - assert histogram("P") == (256, 0, 1871) + assert histogram("P") == (256, 0, 1551) assert histogram("RGB") == (768, 4, 675) assert histogram("RGBA") == (1024, 0, 16384) assert histogram("CMYK") == (1024, 0, 16384) diff --git a/docs/releasenotes/9.2.0.rst b/docs/releasenotes/9.2.0.rst index 424fd487a29..83ba28df85b 100644 --- a/docs/releasenotes/9.2.0.rst +++ b/docs/releasenotes/9.2.0.rst @@ -76,7 +76,9 @@ TODO Other Changes ============= -TODO -^^^^ +Fixed bug with rounding pixels to palette +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -TODO +Fixed a bug when rounding image pixels to palette colors. This affects the results of +``Image.quantize`` and of ``Image.convert`` when converting from mode "RGB" to "P", +"L", or "1", regardless of whether dithering is enabled. diff --git a/src/libImaging/Palette.c b/src/libImaging/Palette.c index 20c6bc84b1a..71a095c2cb8 100644 --- a/src/libImaging/Palette.c +++ b/src/libImaging/Palette.c @@ -200,15 +200,15 @@ ImagingPaletteCacheUpdate(ImagingPalette palette, int r, int g, int b) { /* Find min and max distances to any point in the box */ r = palette->palette[i * 4 + 0]; - tmin = (r < r0) ? RDIST(r, r1) : (r > r1) ? RDIST(r, r0) : 0; + tmin = (r < r0) ? RDIST(r, r0) : (r > r1) ? RDIST(r, r1) : 0; tmax = (r <= rc) ? RDIST(r, r1) : RDIST(r, r0); g = palette->palette[i * 4 + 1]; - tmin += (g < g0) ? GDIST(g, g1) : (g > g1) ? GDIST(g, g0) : 0; + tmin += (g < g0) ? GDIST(g, g0) : (g > g1) ? GDIST(g, g1) : 0; tmax += (g <= gc) ? GDIST(g, g1) : GDIST(g, g0); b = palette->palette[i * 4 + 2]; - tmin += (b < b0) ? BDIST(b, b1) : (b > b1) ? BDIST(b, b0) : 0; + tmin += (b < b0) ? BDIST(b, b0) : (b > b1) ? BDIST(b, b1) : 0; tmax += (b <= bc) ? BDIST(b, b1) : BDIST(b, b0); dmin[i] = tmin;