Code

r16552@tres: ted | 2007-09-11 22:54:20 -0700
[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         
40         # exbed the first embedded image
41         path = self.options.filepath
42         if (path != ''):
43             if (self.options.ids):
44                 for id, node in self.selected.iteritems():
45                     if node.tag == inkex.addNS('image','svg'):
46                         xlink = node.get(inkex.addNS('href','xlink'))
47                         if (xlink[:4]=='data'):
48                             comma = xlink.find(',')
49                             if comma>0:
50                                 #get extension
51                                 fileext=''
52                                 semicolon = xlink.find(';')
53                                 if semicolon>0:
54                                     for sub in mimesubext.keys():
55                                         if sub in xlink[5:semicolon]:
56                                             fileext=mimesubext[sub]
57                                             path=path+fileext;
58                                             break
59                                 #save
60                                 data = base64.decodestring(xlink[comma:])
61                                 open(path,'wb').write(data)
62                                 node.set(inkex.addNS('href','xlink'),os.path.realpath(path)) #absolute for making in-mem cycles work
63                             else:
64                                 inkex.debug('Difficulty finding the image data.')
65                             break
67 e = MyEffect()
68 e.affect()