aboutsummaryrefslogtreecommitdiff
path: root/mammoth/images.py
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2017-04-22 21:04:25 +0100
committerMichael Williamson <mike@zwobble.org>2017-04-22 21:05:46 +0100
commitc1eeb07720c452209d8c35b38aec9ae2bd76ca82 (patch)
treeeb0c7ca546248233ca12446edaba07a31dd4760b /mammoth/images.py
parent324ec7fd20824b0685bc0626f718a26e83dac6c3 (diff)
Extract data URI image converter to images module
Diffstat (limited to 'mammoth/images.py')
-rw-r--r--mammoth/images.py12
1 files changed, 12 insertions, 0 deletions
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)
+ }