Code

made it callable by other extensions. the workaround is sorta ugly, but it should...
[inkscape.git] / share / extensions / extractimage.py
1 #!/usr/bin/env python 
2 '''
3 Copyright (C) 2005 Aaron Spike, aaron@ekips.org
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 '''
20 import inkex, base64, os
22 class MyEffect(inkex.Effect):
23     def __init__(self):
24         inkex.Effect.__init__(self)
25         self.OptionParser.add_option("--desc")
26         self.OptionParser.add_option("--filepath",
27                         action="store", type="string", 
28                         dest="filepath", default=None,
29                         help="")
30     def effect(self):
31         mimesubext={
32             'png' :'.png',
33             'bmp' :'.bmp',
34             'jpeg':'.jpg',
35             'jpg' :'.jpg', #bogus mime
36             'icon':'.ico',
37             'gif' :'.gif'
38         }
39         #ctx = inkex.xml.xpath.Context.Context(self.document,processorNss=inkex.NSS)
40         
41         # exbed the first embedded image
42         path = self.options.filepath
43         if (path != ''):
44             if (self.options.ids):
45                 for id, node in self.selected.iteritems():
46                     if node.tagName == 'image':
47                         xlink = node.attributes.getNamedItemNS(inkex.NSS[u'xlink'],'href')
48                         if (xlink.value[:4]=='data'):
49                             comma = xlink.value.find(',')
50                             if comma>0:
51                                 #get extension
52                                 fileext=''
53                                 semicolon = xlink.value.find(';')
54                                 if semicolon>0:
55                                     for sub in mimesubext.keys():
56                                         if sub in xlink.value[5:semicolon]:
57                                             fileext=mimesubext[sub]
58                                             path=path+fileext;
59                                             break
60                                 #save
61                                 data = base64.decodestring(xlink.value[comma:])
62                                 open(path,'wb').write(data)
63                                 xlink.value = os.path.realpath(path) #absolute for making in-mem cycles work
64                             else:
65                                 inkex.debug('Difficulty finding the image data.')
66                             break
68 e = MyEffect()
69 e.affect()