Commit 92e9f2a3 authored by Ingo Heimbach's avatar Ingo Heimbach

`EmbeddedImage` directive supports svg (for writers that can handle svg)

parent 16325f8c
......@@ -16,6 +16,7 @@ def is_url(url):
def convert_image_to_pillow_image_and_html_base64(image_filepath_or_url):
image_is_svg = image_filepath_or_url.lower().endswith(".svg")
if is_url(image_filepath_or_url):
image_url = image_filepath_or_url
response = requests.get(image_url)
......@@ -25,8 +26,13 @@ def convert_image_to_pillow_image_and_html_base64(image_filepath_or_url):
image_filepath = image_filepath_or_url
with open(image_filepath.encode(sys.getfilesystemencoding()), "rb") as image_file:
image_content = image_file.read()
image = Image.open(BytesIO(image_content))
encoded_string = "data:image/{format};base64,".format(format=image.format.lower()) + base64.b64encode(image_content)
if image_is_svg:
image = None
image_format = "svg+xml"
else:
image = Image.open(BytesIO(image_content))
image_format = image.format.lower()
encoded_string = "data:image/{format};base64,".format(format=image_format) + base64.b64encode(image_content)
return image, encoded_string
......@@ -59,7 +65,7 @@ class EmbeddedImage(Directive):
image, encoded_string = convert_image_to_pillow_image_and_html_base64(image_filepath)
reference = directives.uri(encoded_string)
self.options["uri"] = reference
if not any(attr in self.options for attr in ("height", "width", "scale")):
if not any(attr in self.options for attr in ("height", "width", "scale")) and image is not None:
self.options["width"] = "{}px".format(image.size[0])
reference_node = None
if "target" in self.options:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment