From c855584dac78c8782524f83ef25ae08aa6a66538 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 25 Nov 2023 17:30:09 +1100 Subject: [PATCH 1/2] Corrected "optimize" default when saving GIF images --- docs/handbook/image-file-formats.rst | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/docs/handbook/image-file-formats.rst b/docs/handbook/image-file-formats.rst index fe310df6443..38c00f8702c 100644 --- a/docs/handbook/image-file-formats.rst +++ b/docs/handbook/image-file-formats.rst @@ -266,9 +266,12 @@ following options are available:: :py:class:`PIL.ImagePalette.ImagePalette` object. **optimize** - If present and true, attempt to compress the palette by - eliminating unused colors. This is only useful if the palette can - be compressed to the next smaller power of 2 elements. + Whether to attempt to compress the palette by eliminating unused colors. + This is attempted by default, unless a palette is specified as an option or + as part of the first image's :py:attr:`~PIL.Image.Image.info` dictionary. + + This is only useful if the palette can be compressed to the next smaller + power of 2 elements. Note that if the image you are saving comes from an existing GIF, it may have the following properties in its :py:attr:`~PIL.Image.Image.info` dictionary. From f59b51fdc7c9bac5926a04475f5a371b24addc69 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 25 Nov 2023 17:36:19 +1100 Subject: [PATCH 2/2] Simplified code --- src/PIL/GifImagePlugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py index bdea02005e7..0793d4b4277 100644 --- a/src/PIL/GifImagePlugin.py +++ b/src/PIL/GifImagePlugin.py @@ -656,7 +656,7 @@ def _save(im, fp, filename, save_all=False): palette = im.encoderinfo.get("palette", im.info.get("palette")) else: palette = None - im.encoderinfo["optimize"] = im.encoderinfo.get("optimize", True) + im.encoderinfo.setdefault("optimize", True) if not save_all or not _write_multiple_frames(im, fp, palette): _write_single_frame(im, fp, palette)