From 9dec9177828569a7a8754f99ed2011bf19a6a213 Mon Sep 17 00:00:00 2001 From: Hrishikesh Barman Date: Sat, 24 Aug 2024 19:40:58 +0530 Subject: [PATCH] fix: image conversion for other image modes (skip alpha band) (#712) pillow image modes: https://pillow.readthedocs.io/en/stable/handbook/concepts.html In my case, i was dealing with a `P` mode image. and got hit with `IndexError: tuple index out of range`. While the current operation is useful it's not needed for cases such as the one I faced. So we put it under a condition. --- PyPDFForm/image.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/PyPDFForm/image.py b/PyPDFForm/image.py index 040cba6e..cadeb1a1 100644 --- a/PyPDFForm/image.py +++ b/PyPDFForm/image.py @@ -42,8 +42,11 @@ def any_image_to_jpg(image_stream: bytes) -> bytes: return image_stream rgb_image = Image.new("RGB", image.size, (255, 255, 255)) - rgb_image.paste(image, mask=image.split()[3]) - + if len(image.split()) == 4: + rgb_image.paste(image, mask=image.split()[3]) + else: + rgb_image.paste(image) + with BytesIO() as _file: rgb_image.save(_file, format="JPEG") _file.seek(0)