From bf96f5e858e01535f0e09f81af8513866230b335 Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Sun, 15 Nov 2020 10:36:45 +0100 Subject: [PATCH] Implement function to embed images within SVG file --- src/wireviz/svgembed.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/wireviz/svgembed.py diff --git a/src/wireviz/svgembed.py b/src/wireviz/svgembed.py new file mode 100644 index 0000000..94f5670 --- /dev/null +++ b/src/wireviz/svgembed.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import re +import base64 +from pathlib import Path + +def embed_svg_images(fn): + fn = Path(fn).resolve() + images_b64 = {} # cache base-64 encoded images + num_images = 0 # just for debugging + re_xlink=re.compile(r"xlink:href=\"(?P.*?)\"") + with open(fn) as file_in, open(f'{fn.with_suffix("")}.b64.svg','w') as file_out: + for line in file_in: + for xlink in re_xlink.finditer(line): + num_images = num_images + 1 + imgurl = xlink.group(1) + print(' Found URL in SVG:', imgurl) + if not imgurl in images_b64: + print(' ✅This URL is new') + with open(imgurl, 'rb') as img: + data_bin = img.read() + data_b64 = base64.b64encode(data_bin) + data_str = data_b64.decode('utf-8') + images_b64[imgurl] = data_str + else: # only cache every image once + print(' ❌This URL is not new') + line = line.replace(imgurl, f'data:image/png;base64, {images_b64[imgurl]}') + file_out.write(line) + + print(f'Embedded {num_images} instances of {len(images_b64)} different images.') + print() + +# for debugging, run: +# python -m svgembed.py path/to/file.svg +if __name__ == '__main__': + import sys + embed_svg_images(sys.argv[1])