Skip to content

Commit

Permalink
Fixed palette index for zeroed color after FASTOCTREE quantize
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Dec 5, 2021
1 parent fa2bbf7 commit 456aab1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Tests/test_image_quantize.py
Expand Up @@ -85,3 +85,28 @@ def test_transparent_colors_equal():
converted = im.quantize()
converted_px = converted.load()
assert converted_px[0, 0] == converted_px[0, 1]


@pytest.mark.parametrize(
"method, color",
(
(Image.MEDIANCUT, (0, 0, 0)),
(Image.MAXCOVERAGE, (0, 0, 0)),
(Image.FASTOCTREE, (0, 0, 0)),
(Image.LIBIMAGEQUANT, (0, 0, 0)),
(Image.FASTOCTREE, (0, 0, 0, 0)),
(Image.LIBIMAGEQUANT, (0, 0, 0, 0)),
),
)
def test_palette(method, color):
im = Image.new("RGBA" if len(color) == 4 else "RGB", (1, 1), color)

try:
converted = im.quantize(method=method)
except ValueError as ex: # pragma: no cover
if "dependency" in str(ex).lower():
pytest.skip("libimagequant support not available")
else:
raise
converted_px = converted.load()
assert converted_px[0, 0] == converted.palette.colors[color]
7 changes: 7 additions & 0 deletions src/PIL/Image.py
Expand Up @@ -1112,6 +1112,13 @@ def quantize(self, colors=256, method=None, kmeans=0, palette=None, dither=1):

mode = im.im.getpalettemode()
im.palette = ImagePalette.ImagePalette(mode, im.im.getpalette(mode, mode))
if method == FASTOCTREE:
mode_len = len(mode)
color = (0,) * mode_len
if color in im.palette.colors and im.palette.palette.endswith(
b"\x00" * mode_len
):
im.palette.colors[color] = 255

return im

Expand Down

0 comments on commit 456aab1

Please sign in to comment.