Clarify check that palette is not already at its smallest

This commit is contained in:
Andrew Murray 2022-06-26 20:58:36 +10:00
parent 709744432a
commit 847ad8c512

View File

@ -830,9 +830,13 @@ def _get_optimize(im, info):
num_palette_colors = len(im.palette.palette) // Image.getmodebands( num_palette_colors = len(im.palette.palette) // Image.getmodebands(
im.palette.mode im.palette.mode
) )
# Round up to power of 2 but at least 4 current_palette_size = 1 << (num_palette_colors - 1).bit_length()
num_palette_colors = max(4, 1 << (num_palette_colors - 1).bit_length()) if (
if len(used_palette_colors) <= num_palette_colors // 2: # check that the palette would become smaller when saved
len(used_palette_colors) <= current_palette_size // 2
# check that the palette is not already the smallest possible size
and current_palette_size > 2
):
return used_palette_colors return used_palette_colors