Code

Attempt fix for Bug #178022
[inkscape.git] / share / extensions / gimp_xcf.py
1 #!/usr/bin/env python 
2 '''
3 Copyright (C) 2006 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 '''
19 import inkex
20 import sys, os, tempfile
22 class MyEffect(inkex.Effect):
23     def __init__(self):
24         inkex.Effect.__init__(self)
25     def output(self):
26         pass
27     def effect(self):
28         svg_file = self.args[-1]
29         docname = self.xpathSingle('/svg:svg/@sodipodi:docname')[:-4]
31         #create os temp dir
32         tmp_dir = tempfile.mkdtemp()
34         area = '--export-area-canvas'
35         pngs = []
36         names = []
37         path = "/svg:svg/*[name()='g' or @style][@id]"
38         for node in self.document.xpath(path, namespaces=inkex.NSS):
39             id = node.get('id')
40             name = "%s.png" % id
41             filename = os.path.join(tmp_dir, name)
42             command = "inkscape -i %s -j %s -e %s %s " % (id, area, filename, svg_file)
43             _,f,err = os.popen3(command,'r')
44             f.read()
45             f.close()
46             err.close()
47             pngs.append(filename)
48             names.append(id)
50         filelist = '"%s"' % '" "'.join(pngs)
51         namelist = '"%s"' % '" "'.join(names)
52         xcf = os.path.join(tmp_dir, "%s.xcf" % docname)
53         script_fu = """
54 (tracing 1)
55 (define
56   (png-to-layer img png_filename layer_name)
57   (let*
58     (
59       (png (car (file-png-load RUN-NONINTERACTIVE png_filename png_filename)))
60       (png_layer (car (gimp-image-get-active-layer png)))
61       (xcf_layer (car (gimp-layer-new-from-drawable png_layer img)))
62     )
63     (gimp-image-add-layer img xcf_layer -1)
64     (gimp-drawable-set-name xcf_layer layer_name)
65   )
66 )
67 (let*
68   (
69     (img (car (gimp-image-new 200 200 RGB)))
70   )
71   (gimp-image-undo-disable img)
72   (for-each
73     (lambda (names)
74       (png-to-layer img (car names) (cdr names))
75     )
76     (map cons '(%s) '(%s))
77   )
78   (gimp-image-resize-to-layers img)
79   (gimp-image-undo-enable img)
80   (gimp-file-save RUN-NONINTERACTIVE img (car (gimp-image-get-active-layer img)) "%s" "%s"))
81 (gimp-quit 0)
82         """ % (filelist, namelist, xcf, xcf)
84         junk = os.path.join(tmp_dir, 'junk_from_gimp.txt')
85         f = os.popen('gimp -i --batch-interpreter plug-in-script-fu-eval -b - > %s 2>&1' % junk,'w')
86         f.write(script_fu)
87         f.close()
88         # uncomment these lines to see the output from gimp
89         #err = open(junk, 'r')
90         #inkex.debug(err.read())
91         #err.close()
93         x = open(xcf, 'r')
94         sys.stdout.write(x.read())
95         x.close()
97 e = MyEffect()
98 e.affect()