From 408bff58955d191e5e22b89452a94b7da2f7b414 Mon Sep 17 00:00:00 2001 From: Martin Thoma Date: Sat, 9 Jul 2022 11:20:55 +0200 Subject: [PATCH] Add Indexed color space support --- PyPDF2/filters.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/PyPDF2/filters.py b/PyPDF2/filters.py index 7aaca877b..543844454 100644 --- a/PyPDF2/filters.py +++ b/PyPDF2/filters.py @@ -570,7 +570,21 @@ def _xobj_to_image(x_object_obj: Dict[str, Any]) -> Tuple[Optional[str], bytes]: if SA.FILTER in x_object_obj: if x_object_obj[SA.FILTER] == FT.FLATE_DECODE: extension = ".png" + color_space = None + if "/ColorSpace" in x_object_obj: + color_space = x_object_obj["/ColorSpace"].get_object() + if ( + isinstance(color_space, ArrayObject) + and color_space[0] == "/Indexed" + ): + color_space, base, hival, lookup = ( + value.get_object() for value in color_space + ) + img = Image.frombytes(mode, size, data) + if color_space == "/Indexed": + img.putpalette(lookup.get_data()) + img = img.convert("RGB") if G.S_MASK in x_object_obj: # add alpha channel alpha = Image.frombytes("L", size, x_object_obj[G.S_MASK].get_data()) img.putalpha(alpha)