From d39df4e614ef7d31307b5f8ba9036bbf18a2dd63 Mon Sep 17 00:00:00 2001 From: Mario Voigt Date: Tue, 19 Oct 2021 02:20:26 +0200 Subject: [PATCH] added webp image import extension --- .../fablabchemnitz/webp_import/meta.json | 20 ++++++ .../fablabchemnitz/webp_import/output.xml | 0 .../fablabchemnitz/webp_import/person.xml | 1 + .../webp_import/webp_import.inx | 14 ++++ .../fablabchemnitz/webp_import/webp_import.py | 72 +++++++++++++++++++ 5 files changed, 107 insertions(+) create mode 100644 extensions/fablabchemnitz/webp_import/meta.json create mode 100644 extensions/fablabchemnitz/webp_import/output.xml create mode 100644 extensions/fablabchemnitz/webp_import/person.xml create mode 100644 extensions/fablabchemnitz/webp_import/webp_import.inx create mode 100644 extensions/fablabchemnitz/webp_import/webp_import.py diff --git a/extensions/fablabchemnitz/webp_import/meta.json b/extensions/fablabchemnitz/webp_import/meta.json new file mode 100644 index 00000000..e74d9753 --- /dev/null +++ b/extensions/fablabchemnitz/webp_import/meta.json @@ -0,0 +1,20 @@ +[ + { + "name": "WebP Import", + "id": "fablabchemnitz.de.webp_import", + "path": "webp_import", + "original_name": "WebP Import", + "original_id": "fablabchemnitz.de.webp_import", + "license": "GNU GPL v3", + "license_url": "https://gitea.fablabchemnitz.de/FabLab_Chemnitz/mightyscape-1.X/src/branch/master/LICENSE", + "comment": "Written by Mario Voigt", + "source_url": "https://gitea.fablabchemnitz.de/FabLab_Chemnitz/mightyscape-1.X/src/branch/master/extensions/fablabchemnitz/webp_import", + "fork_url": null, + "documentation_url": "https://stadtfabrikanten.org/display/IFM/WebP+Import", + "inkscape_gallery_url": null, + "main_authors": [ + "inkscape.org/mono", + "github.com/vmario89" + ] + } +] \ No newline at end of file diff --git a/extensions/fablabchemnitz/webp_import/output.xml b/extensions/fablabchemnitz/webp_import/output.xml new file mode 100644 index 00000000..e69de29b diff --git a/extensions/fablabchemnitz/webp_import/person.xml b/extensions/fablabchemnitz/webp_import/person.xml new file mode 100644 index 00000000..572613cf --- /dev/null +++ b/extensions/fablabchemnitz/webp_import/person.xml @@ -0,0 +1 @@ +5.1 \ No newline at end of file diff --git a/extensions/fablabchemnitz/webp_import/webp_import.inx b/extensions/fablabchemnitz/webp_import/webp_import.inx new file mode 100644 index 00000000..2f83bc2c --- /dev/null +++ b/extensions/fablabchemnitz/webp_import/webp_import.inx @@ -0,0 +1,14 @@ + + + WebP Import + fablabchemnitz.de.webp_import + + .webp + image/webp + WebP Format (*.webp) + Import WebP Format + + + diff --git a/extensions/fablabchemnitz/webp_import/webp_import.py b/extensions/fablabchemnitz/webp_import/webp_import.py new file mode 100644 index 00000000..51b6fe5f --- /dev/null +++ b/extensions/fablabchemnitz/webp_import/webp_import.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 + +import sys +import os +import argparse +import inkex +from inkex import Rectangle +from PIL import Image +import base64 +from io import BytesIO, StringIO +from distutils.spawn import find_executable +import subprocess +from lxml import etree + +class WebpImport(inkex.InputExtension): + + def add_arguments(self, pars): + pars.add_argument('inputfile') + + def effect(self): + if os.name == 'nt': + tmp = os.getenv('TEMP') + '\\' + else: + tmp = '/tmp/' + convertfile = os.path.join(tmp, "webp.png") + if find_executable('magick'): + command = "magick \"%s\" \"%s\" " % (self.options.inputfile, convertfile) + elif find_executable('convert'): + command = "convert \"%s\" \"%s\" " % (self.options.inputfile, convertfile) + else: + inkex.errormsg('ImageMagick does not appear to be installed.') + exit() + p = subprocess.Popen(command, shell=True) + return_code = p.wait() + #inkex.utils.debug("command:" + command) + #inkex.utils.debug("Errorcode:" + str(return_code)) + + try: + img = Image.open(convertfile) + except Image.DecompressionBombError as e: #we could also increse PIL.Image.MAX_IMAGE_PIXELS = some large int + self.msg("Error. Image is too large. Reduce DPI and try again!") + exit(1) + output_buffer = BytesIO() + img.save(output_buffer, format='PNG') + width, height = img.size + byte_data = output_buffer.getvalue() + base64_str = base64.b64encode(byte_data).decode('UTF-8') + webp = etree.SubElement(Rectangle(), '{http://www.w3.org/2000/svg}image') + webp.attrib['x'] = str(0) + webp.attrib['y'] = str(0) + webp.attrib['width'] = str(width) + webp.attrib['height'] = str(height) + webp.attrib['{http://www.w3.org/1999/xlink}href'] = "data:image/png;base64,{}".format(base64_str) + base = ('' + ).format(width, height, 0, 0, width, height) + output = StringIO(base) + tree = etree.parse(output) + output.close() + tree.getroot().append(webp) + svgfile = os.path.join(tmp, "webp.svg") + with open(svgfile, 'w') as file: + tree.write(svgfile) + with open(svgfile, 'r') as newfile: + sys.stdout.write(newfile.read()) + + def load(self, stream): + return str(stream.read()) + +if __name__ == '__main__': + WebpImport().run() + \ No newline at end of file