From d9345ac76767ef3b9bf2601d4a747f7babff88ce Mon Sep 17 00:00:00 2001 From: Johannes Schauer Marin Rodrigues Date: Thu, 7 Apr 2022 21:57:37 +0200 Subject: [PATCH] set GifImagePlugin.LOADING_STRATEGY to RGB_AFTER_DIFFERENT_PALETTE_ONLY in Pillow >= 9.1.0 (closes: #130) --- src/img2pdf.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/img2pdf.py b/src/img2pdf.py index b84fc24..09c0d63 100755 --- a/src/img2pdf.py +++ b/src/img2pdf.py @@ -22,7 +22,17 @@ import sys import os import zlib import argparse -from PIL import Image, TiffImagePlugin +from PIL import Image, TiffImagePlugin, GifImagePlugin + +if hasattr(GifImagePlugin, "LoadingStrategy"): + # Pillow 9.0.0 started emitting all frames but the first as RGB instead of + # P to make sure that more than 256 colors can be represented. But palette + # images compress far better than RGB images in PDF so we instruct Pillow + # to only emit RGB frames if the palette differs and return P otherwise. + # This works since Pillow 9.1.0. + GifImagePlugin.LOADING_STRATEGY = ( + GifImagePlugin.LoadingStrategy.RGB_AFTER_DIFFERENT_PALETTE_ONLY + ) # TiffImagePlugin.DEBUG = True from PIL.ExifTags import TAGS