home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Inkscape / Inkscape-0.48.2-1-win32.exe / share / extensions / extractimage.py < prev    next >
Text File  |  2011-07-08  |  3KB  |  81 lines

  1. #!/usr/bin/env python 
  2. '''
  3. Copyright (C) 2005 Aaron Spike, aaron@ekips.org
  4.  
  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.
  9.  
  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.
  14.  
  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. '''
  19.  
  20. import inkex, base64, os
  21. import gettext
  22. _ = gettext.gettext
  23.  
  24. class MyEffect(inkex.Effect):
  25.     def __init__(self):
  26.         inkex.Effect.__init__(self)
  27.         self.OptionParser.add_option("--desc")
  28.         self.OptionParser.add_option("--filepath",
  29.                         action="store", type="string", 
  30.                         dest="filepath", default=None,
  31.                         help="")
  32.     def effect(self):
  33.         mimesubext={
  34.             'png' :'.png',
  35.             'bmp' :'.bmp',
  36.             'jpeg':'.jpg',
  37.             'jpg' :'.jpg', #bogus mime
  38.             'icon':'.ico',
  39.             'gif' :'.gif'
  40.         }
  41.         
  42.         # exbed the first embedded image
  43.         path = self.options.filepath
  44.         if (path != ''):
  45.             if (self.options.ids):
  46.                 for id, node in self.selected.iteritems():
  47.                     if node.tag == inkex.addNS('image','svg'):
  48.                         xlink = node.get(inkex.addNS('href','xlink'))
  49.                         if (xlink[:4]=='data'):
  50.                             comma = xlink.find(',')
  51.                             if comma>0:
  52.                                 #get extension
  53.                                 fileext=''
  54.                                 semicolon = xlink.find(';')
  55.                                 if semicolon>0:
  56.                                     for sub in mimesubext.keys():
  57.                                         if sub in xlink[5:semicolon].lower():
  58.                                             fileext=mimesubext[sub]
  59.                                             path=path+fileext;
  60.                                             if (not os.path.isabs(path)):
  61.                                                 if os.name == 'nt':
  62.                                                     path = os.path.join(os.environ['USERPROFILE'],path)
  63.                                                 else:
  64.                                                     path = os.path.join(os.path.expanduser("~"),path)
  65.                                             inkex.errormsg(_('Image extracted to: %s') % path)
  66.                                             break 
  67.                                 #save
  68.                                 data = base64.decodestring(xlink[comma:])
  69.                                 open(path,'wb').write(data)
  70.                                 node.set(inkex.addNS('href','xlink'),os.path.realpath(path)) #absolute for making in-mem cycles work
  71.                             else:
  72.                                 inkex.errormsg(_('Unable to find image data.'))
  73.                             break
  74.  
  75. if __name__ == '__main__':
  76.     e = MyEffect()
  77.     e.affect()
  78.  
  79.  
  80. # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99
  81.