diff options
| author | Michael Williamson <mike@zwobble.org> | 2013-12-14 16:48:59 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2013-12-14 16:48:59 +0000 |
| commit | 93e945c2ca0b1118c8e119a09988d8b4093625d8 (patch) | |
| tree | b12810548cffc620c4bef09b364c8f6bd43373b5 /mammoth | |
| parent | 6f9267b715dc2d0e9c722918c074a8cd6a35e2e3 (diff) | |
Convert image using base64 encoding
Diffstat (limited to 'mammoth')
| -rw-r--r-- | mammoth/conversion.py | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/mammoth/conversion.py b/mammoth/conversion.py index 72867c5..5580925 100644 --- a/mammoth/conversion.py +++ b/mammoth/conversion.py @@ -1,3 +1,6 @@ +import base64 +import io + from . import documents, results, html_paths from .html_generation import HtmlGenerator, satisfy_html_path @@ -23,6 +26,7 @@ class DocumentConverter(object): documents.Text: self._convert_text, documents.Hyperlink: self._convert_hyperlink, documents.Tab: self._convert_tab, + documents.Image: self._convert_image, } @@ -66,6 +70,17 @@ class DocumentConverter(object): def _convert_tab(self, tab, html_generator): html_generator.text("\t") + + + def _convert_image(self, image, html_generator): + with image.open() as image_bytes: + output = io.BytesIO() + base64.encode(image_bytes, output) + encoded_src = output.getvalue().rstrip("\n") + + html_generator.self_closing("img", { + "src": "data:{0};base64,{1}".format(image.content_type, encoded_src) + }) def _convert_elements_to_html(self, elements, html_generator): |
