Skip to content

Commit

Permalink
fix: image conversion for other image modes (skip alpha band) (#712)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
geekodour authored Aug 24, 2024
1 parent 7797d48 commit 9dec917
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions PyPDFForm/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9dec917

Please sign in to comment.