added webp image import extension
This commit is contained in:
parent
f8513e5e01
commit
d39df4e614
20
extensions/fablabchemnitz/webp_import/meta.json
Normal file
20
extensions/fablabchemnitz/webp_import/meta.json
Normal file
@ -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"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
0
extensions/fablabchemnitz/webp_import/output.xml
Normal file
0
extensions/fablabchemnitz/webp_import/output.xml
Normal file
1
extensions/fablabchemnitz/webp_import/person.xml
Normal file
1
extensions/fablabchemnitz/webp_import/person.xml
Normal file
@ -0,0 +1 @@
|
|||||||
|
<QuoteWerksXML><AppVersionMajor>5.1</AppVersionMajor></QuoteWerksXML>
|
14
extensions/fablabchemnitz/webp_import/webp_import.inx
Normal file
14
extensions/fablabchemnitz/webp_import/webp_import.inx
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
|
||||||
|
<name>WebP Import</name>
|
||||||
|
<id>fablabchemnitz.de.webp_import</id>
|
||||||
|
<input>
|
||||||
|
<extension>.webp</extension>
|
||||||
|
<mimetype>image/webp</mimetype>
|
||||||
|
<filetypename>WebP Format (*.webp)</filetypename>
|
||||||
|
<filetypetooltip>Import WebP Format</filetypetooltip>
|
||||||
|
</input>
|
||||||
|
<script>
|
||||||
|
<command location="inx" interpreter="python">webp_import.py</command>
|
||||||
|
</script>
|
||||||
|
</inkscape-extension>
|
72
extensions/fablabchemnitz/webp_import/webp_import.py
Normal file
72
extensions/fablabchemnitz/webp_import/webp_import.py
Normal file
@ -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 = ('<svg xmlns="http://www.w3.org/2000/svg"'
|
||||||
|
' width="{}px" height="{}px" viewBox="{} {} {} {}"/>'
|
||||||
|
).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()
|
||||||
|
|
Reference in New Issue
Block a user