From c1eeb07720c452209d8c35b38aec9ae2bd76ca82 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 22 Apr 2017 21:04:25 +0100 Subject: Extract data URI image converter to images module --- mammoth/images.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'mammoth/images.py') diff --git a/mammoth/images.py b/mammoth/images.py index 297b0a2..9dba353 100644 --- a/mammoth/images.py +++ b/mammoth/images.py @@ -1,3 +1,5 @@ +import base64 + from . import html @@ -13,3 +15,13 @@ def img_element(func): # Undocumented, but retained for backwards-compatibility with 0.3.x inline = img_element + + +@img_element +def data_uri(image): + with image.open() as image_bytes: + encoded_src = base64.b64encode(image_bytes.read()).decode("ascii") + + return { + "src": "data:{0};base64,{1}".format(image.content_type, encoded_src) + } -- cgit v1.2.3