From 484d3660a34cfedb24431aed8ecbb61a30afb193 Mon Sep 17 00:00:00 2001 From: wgundamj44 Date: Fri, 15 Dec 2023 22:27:02 +0900 Subject: [PATCH] make xlink_href_target method create PIL image instead of temporary file --- svglib/svglib.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/svglib/svglib.py b/svglib/svglib.py index 88dac27..894311f 100755 --- a/svglib/svglib.py +++ b/svglib/svglib.py @@ -21,10 +21,11 @@ import os import pathlib import re -import tempfile import shlex import shutil +from io import BytesIO from collections import defaultdict, namedtuple +from PIL import Image as PILImage from reportlab.pdfbase.pdfmetrics import stringWidth from reportlab.pdfgen.canvas import FILL_EVEN_ODD, FILL_NON_ZERO @@ -679,7 +680,7 @@ def xlink_href_target(self, node, group=None): Return either: - a tuple (renderer, node) when the the xlink:href attribute targets a vector file or node - - the path to an image file for any raster image targets + - a PIL Image object representing the image file - None if any problem occurs """ # Bare 'href' was introduced in SVG 2. @@ -690,16 +691,10 @@ def xlink_href_target(self, node, group=None): # First handle any raster embedded image data match = re.match(r"^data:image/(jpe?g|png);base64", xlink_href) if match: - img_format = match.groups()[0] image_data = base64.decodebytes(xlink_href[(match.span(0)[1] + 1):].encode('ascii')) - file_indicator, path = tempfile.mkstemp(suffix=f'.{img_format}') - with open(path, 'wb') as fh: - fh.write(image_data) - # Close temporary file (as opened by tempfile.mkstemp) - os.close(file_indicator) - # this needs to be removed later, not here... - # if exists(path): os.remove(path) - return path + bytes_stream = BytesIO(image_data) + + return PILImage.open(bytes_stream) # From here, we can assume this is a path. if '#' in xlink_href: