Commit 64d9a7d9 authored by Aaron Spike's avatar Aaron Spike Committed by acspike

Adding Pim Snel's svg_and_media_zip_output extension. Adding an extract image...

Adding Pim Snel's svg_and_media_zip_output extension. Adding an extract image extension. Adding a license to embed image.

(bzr r8352)
parent 83b0d5f8
......@@ -42,7 +42,9 @@ extensions = \
addnodes.py \
summersnight.py \
ps2dxf.sh \
embedimage.py
embedimage.py \
extractimage.py \
svg_and_media_zip_output.py
otherstuff =
......@@ -80,7 +82,9 @@ modules = \
summersnight.inx \
dxf_output.inx \
dxf_input.inx \
embedimage.inx
embedimage.inx \
extractimage.inx \
svg_and_media_zip_output.inx
extension_SCRIPTS = \
$(extensions)
......
#!/usr/bin/env python
'''
Copyright (C) 2005 Aaron Spike, aaron@ekips.org
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'''
import inkex, os, base64
class MyEffect(inkex.Effect):
......
<inkscape-extension>
<name>Extract One Image</name>
<id>org.ekips.filter.extractimage</id>
<dependency type="executable" location="extensions">extractimage.py</dependency>
<dependency type="executable" location="extensions">inkex.py</dependency>
<param name="filepath" type="string" gui-text="Path to Save Image">none</param>
<effect>
<object-type>all</object-type>
</effect>
<script>
<command reldir="extensions" interpreter="python">extractimage.py</command>
</script>
</inkscape-extension>
#!/usr/bin/env python
'''
Copyright (C) 2005 Aaron Spike, aaron@ekips.org
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'''
import inkex, base64
class MyEffect(inkex.Effect):
def __init__(self):
inkex.Effect.__init__(self)
self.OptionParser.add_option("--filepath",
action="store", type="string",
dest="filepath", default=None,
help="")
def effect(self):
ctx = inkex.xml.xpath.Context.Context(self.document,processorNss=inkex.NSS)
# exbed the first embedded image
path = self.options.filepath
if (path != ''):
if (self.options.ids):
for id, node in self.selected.iteritems():
if node.tagName == 'image':
xlink = node.attributes.getNamedItemNS(inkex.NSS[u'xlink'],'href')
if (xlink.value[:4]=='data'):
comma = xlink.value.find(',')
if comma>0:
data = base64.decodestring(xlink.value[comma:])
open(path,'wb').write(data)
xlink.value = path
else:
inkex.debug('Difficulty finding the image data.')
break
e = MyEffect()
e.affect()
<inkscape-extension>
<name>ZIP Output</name>
<id>org.inkscape.output.ZIP</id>
<dependency type="extension">org.inkscape.output.svg.inkscape</dependency>
<dependency type="executable" location="extensions">svg_and_media_zip_output.py</dependency>
<dependency type="executable" location="extensions">inkex.py</dependency>
<output>
<extension>.zip</extension>
<mimetype>application/x-zip</mimetype>
<filetypename>Compressed Inkscape SVG with media (*.zip)</filetypename>
<filetypetooltip>Inkscape's native file format compressed with Zip and including all media files</filetypetooltip>
<dataloss>FALSE</dataloss>
</output>
<script>
<command reldir="extensions" interpreter="python">svg_and_media_zip_output.py</command>
<helper_extension>org.inkscape.output.svg.inkscape</helper_extension>
</script>
</inkscape-extension>
#!/usr/bin/env python
"""
svg_and_media_zip_output.py
An extention which collects all images to the documents directory and
creates a zip archive containing all images and the document
Copyright (C) 2005 Pim Snel, pim@lingewoud.com
this is the first Python script ever created
its based on embedimage.py
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Version 0.3
TODO
- fix bug: not saving existing .zip after a Collect for Output is run
this bug occurs because after running an effect extention the inkscape:output_extension is reset to svg.inkscape
the file name is still xxx.zip. after saving again the file xxx.zip is written with a plain .svg which
looks like a corrupt zip
- maybe add better extention
"""
import inkex, os.path
import os
import string
import zipfile
import shutil
import sys
import tempfile
class MyEffect(inkex.Effect):
def __init__(self):
inkex.Effect.__init__(self)
self.documentDst=None
def parseTmp(self,file=None):
"""Parse document in specified file or on stdin"""
reader = inkex.xml.dom.ext.reader.Sax2.Reader()
try:
try:
stream = open(file,'r')
except:
stream = open(self.args[-1],'r')
except:
stream = sys.stdin
self.documentDst = reader.fromStream(stream)
stream.close()
def output(self):
pass
def effect(self):
#get needed info from orig document
ctx_orig = inkex.xml.xpath.Context.Context(self.document,processorNss=inkex.NSS)
ttmp_orig = inkex.xml.xpath.Evaluate('/svg',self.document, context=ctx_orig)
docbase = ttmp_orig[0].attributes.getNamedItemNS(inkex.NSS[u'sodipodi'],'docbase')
docname = ttmp_orig[0].attributes.getNamedItemNS(inkex.NSS[u'sodipodi'],'docname')
orig_tmpfile = sys.argv[1]
# create destination zip in same directory as the document
z = zipfile.ZipFile(docbase.value + '/'+ docname.value + '.zip', 'w')
#create os temp dir
tmp_dir = tempfile.mkdtemp()
#fixme replace whatever extention
docstripped = docname.value.replace('.zip', '')
#read tmpdoc and copy all images to temp dir
for node in inkex.xml.xpath.Evaluate('//image',self.document, context=ctx_orig):
self.collectAndZipImages(node, tmp_dir, docname, z)
##copy tmpdoc to tempdir
dst_file = os.path.join(tmp_dir, docstripped)
stream = open(dst_file,'w')
inkex.xml.dom.ext.Print(self.document,stream)
stream.close()
z.write(dst_file.encode("latin-1"),docstripped.encode("latin-1")+'.svg')
z.close()
shutil.move(docbase.value + '/'+ docname.value + '.zip',docbase.value + '/'+ docname.value)
shutil.rmtree(tmp_dir)
def collectAndZipImages(self, node, tmp_dir, docname, z):
xlink = node.attributes.getNamedItemNS(inkex.NSS[u'xlink'],'href')
if (xlink.value[:4]!='data'):
absref=node.attributes.getNamedItemNS(inkex.NSS[u'sodipodi'],'absref')
if (os.path.isfile(absref.value)):
shutil.copy(absref.value,tmp_dir)
z.write(absref.value.encode("latin-1"),os.path.basename(absref.value).encode("latin-1"))
elif (os.path.isfile(tmp_dir + '/' + absref.value)):
shutil.copy(tmp_dir + '/' + absref.value,tmp_dir)
z.write(tmp_dir + '/' + absref.value.encode("latin-1"),os.path.basename(absref.value).encode("latin-1"))
xlink.value = os.path.basename(absref.value)
absref.value = os.path.basename(absref.value)
e = MyEffect()
e.affect()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment