Skip to content

Commit e6bfa50

Browse files
committed
Fix PIL-related warning
1 parent 3237aa1 commit e6bfa50

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/svglib/svglib.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@
8282
)
8383

8484

85+
def _convert_palette_to_rgba(image: PILImage.Image) -> PILImage.Image:
86+
"""
87+
Convert palette images with transparency to RGBA to avoid PIL warnings.
88+
89+
Args:
90+
image: PIL Image object
91+
92+
Returns:
93+
PIL Image object converted to RGBA if it was a palette image with transparency
94+
"""
95+
if image.mode == "P" and "transparency" in image.info:
96+
# Convert palette image with transparency to RGBA
97+
return image.convert("RGBA")
98+
return image
99+
100+
85101
def register_font(
86102
font_name: str,
87103
font_path: Optional[str] = None,
@@ -770,7 +786,7 @@ def xlink_href_target(self, node: NodeTracker, group: Optional[Any] = None) -> A
770786
)
771787
bytes_stream = BytesIO(image_data)
772788

773-
return PILImage.open(bytes_stream)
789+
return _convert_palette_to_rgba(PILImage.open(bytes_stream))
774790

775791
# From here, we can assume this is a path.
776792
if "#" in xlink_href:

0 commit comments

Comments
 (0)