extractimage.py revision 80809dee6f6d120234c4570056c9a10c063953d8
e9b6af083e34e2397a8ddbe9781920733d09d151Ted Gould#!/usr/bin/env python
e9b6af083e34e2397a8ddbe9781920733d09d151Ted Gould'''
e9b6af083e34e2397a8ddbe9781920733d09d151Ted GouldCopyright (C) 2005 Aaron Spike, aaron@ekips.org
e9b6af083e34e2397a8ddbe9781920733d09d151Ted Gould
c57da20fee24a6e5b368730153d72eabe26425b2buliabyakThis program is free software; you can redistribute it and/or modify
c57da20fee24a6e5b368730153d72eabe26425b2buliabyakit under the terms of the GNU General Public License as published by
c57da20fee24a6e5b368730153d72eabe26425b2buliabyakthe Free Software Foundation; either version 2 of the License, or
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak(at your option) any later version.
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak
c57da20fee24a6e5b368730153d72eabe26425b2buliabyakThis program is distributed in the hope that it will be useful,
c57da20fee24a6e5b368730153d72eabe26425b2buliabyakbut WITHOUT ANY WARRANTY; without even the implied warranty of
c57da20fee24a6e5b368730153d72eabe26425b2buliabyakMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c57da20fee24a6e5b368730153d72eabe26425b2buliabyakGNU General Public License for more details.
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak
c57da20fee24a6e5b368730153d72eabe26425b2buliabyakYou should have received a copy of the GNU General Public License
c57da20fee24a6e5b368730153d72eabe26425b2buliabyakalong with this program; if not, write to the Free Software
c57da20fee24a6e5b368730153d72eabe26425b2buliabyakFoundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak'''
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak
c57da20fee24a6e5b368730153d72eabe26425b2buliabyakimport inkex, base64
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak
c57da20fee24a6e5b368730153d72eabe26425b2buliabyakclass MyEffect(inkex.Effect):
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak def __init__(self):
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak inkex.Effect.__init__(self)
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak self.OptionParser.add_option("--filepath",
5c8e50345f9a78a6823dff57ba6b75109f81cd77gustav_b action="store", type="string",
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak dest="filepath", default=None,
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak help="")
963f23115db07f460bdd862b957f8bd9dba88b9bgustav_b def effect(self):
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak ctx = inkex.xml.xpath.Context.Context(self.document,processorNss=inkex.NSS)
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak # exbed the first embedded image
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak path = self.options.filepath
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak if (path != ''):
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak if (self.options.ids):
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak for id, node in self.selected.iteritems():
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak if node.tagName == 'image':
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak xlink = node.attributes.getNamedItemNS(inkex.NSS[u'xlink'],'href')
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak if (xlink.value[:4]=='data'):
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak comma = xlink.value.find(',')
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak if comma>0:
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak data = base64.decodestring(xlink.value[comma:])
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak open(path,'wb').write(data)
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak xlink.value = path
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak else:
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak inkex.debug('Difficulty finding the image data.')
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak break
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak
c57da20fee24a6e5b368730153d72eabe26425b2buliabyake = MyEffect()
c57da20fee24a6e5b368730153d72eabe26425b2buliabyake.affect()
c57da20fee24a6e5b368730153d72eabe26425b2buliabyak