Code

964d1a5ecfbeea57d4e583120710ee34b9c489a5
[inkscape.git] / share / extensions / embedimage.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, os, base64
22 class MyEffect(inkex.Effect):
23     def __init__(self):
24         inkex.Effect.__init__(self)
25         self.OptionParser.add_option("-s", "--selectedonly",
26             action="store", type="inkbool", 
27             dest="selectedonly", default=False,
28             help="embed only selected images")
30     def effect(self):
31         ctx = inkex.xml.xpath.Context.Context(self.document,processorNss=inkex.NSS)
33         # if slectedonly is enabled and there is a selection only embed selected
34         # images. otherwise embed all images
35         if (self.options.selectedonly):
36             if (self.options.ids):
37                 for id, node in self.selected.iteritems():
38                     if node.tagName == 'image':
39                         self.embedImage(node)
40         else:
41             path = '//image'
42             for node in inkex.xml.xpath.Evaluate(path,self.document, context=ctx):
43                 self.embedImage(node)
44     def embedImage(self, node):
45         xlink = node.attributes.getNamedItemNS(inkex.NSS[u'xlink'],'href')
46         if (xlink.value[:4]!='data'):
47             absref=node.attributes.getNamedItemNS(inkex.NSS[u'sodipodi'],'absref')
48             if (os.path.isfile(absref.value)):
49                 file = open(absref.value,"rb").read()
50                 embed=True
51                 if (file[:4]=='\x89PNG'):
52                     type='image/png'
53                 elif (file[:2]=='\xff\xd8'):
54                     type='image/jpeg'
55                 elif (file[:2]=='BM'):
56                     type='image/bmp'
57                 elif (file[:6]=='GIF87a' or file[:6]=='GIF89a'):
58                     type='image/gif'
59                 #ico files lack any magic... therefore we check the filename instead
60                 elif(absref.value.endswith('.ico')):
61                     type='image/x-icon' #official IANA registered MIME is 'image/vnd.microsoft.icon' tho
62                 else:
63                     embed=False
64                 if (embed):
65                     xlink.value = 'data:%s;base64,%s' % (type, base64.encodestring(file))
66                     node.removeAttributeNS(inkex.NSS[u'sodipodi'],'absref')
67                 else:
68                     inkex.debug("%s is not of type image/png, image/jpeg, image/bmp, image/gif or image/x-icon" % absref.value)
69             else:
70                 inkex.debug("Sorry we could not locate %s" % absref.value)
71 e = MyEffect()
72 e.affect()