Skip to content

Commit

Permalink
Merge pull request #5879 from radarhere/quantize
Browse files Browse the repository at this point in the history
Limit quantized palette to number of colors
  • Loading branch information
hugovk committed Dec 28, 2021
2 parents 39de053 + ec19889 commit 17ec8b5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Tests/test_image_quantize.py
Expand Up @@ -77,6 +77,13 @@ def test_quantize_dither_diff():
assert dither.tobytes() != nodither.tobytes()


def test_colors():
im = hopper()
colors = 2
converted = im.quantize(colors)
assert len(converted.palette.palette) == colors * len("RGB")


def test_transparent_colors_equal():
im = Image.new("RGBA", (1, 2), (0, 0, 0, 0))
px = im.load()
Expand Down
3 changes: 2 additions & 1 deletion src/PIL/Image.py
Expand Up @@ -1109,7 +1109,8 @@ def quantize(self, colors=256, method=None, kmeans=0, palette=None, dither=1):
from . import ImagePalette

mode = im.im.getpalettemode()
im.palette = ImagePalette.ImagePalette(mode, im.im.getpalette(mode, mode))
palette = im.im.getpalette(mode, mode)[: colors * len(mode)]
im.palette = ImagePalette.ImagePalette(mode, palette)

return im

Expand Down

0 comments on commit 17ec8b5

Please sign in to comment.