Skip to content

Instantly share code, notes, and snippets.

@Ramblurr
Forked from lzubiaur/svgexport.py
Created May 9, 2014 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ramblurr/c6c14fee09979ddf6d13 to your computer and use it in GitHub Desktop.
Save Ramblurr/c6c14fee09979ddf6d13 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: latin-1 -*-
"""
svgexport.py - Batch export for Inkscape - Laurent Zubiaur
description:
Python script that can be used to automate batch exports of Inkscape objects by providing their id (set in the Object Properties dialog).
Also useful to batch export with a specific dpi (only available for individual object export in GUI mode).
For all available inkscape command line options see http://inkscape.org/doc/inkscape-man.html.
usage:
python svgexport.py <path>
argument:
Path to the directory containing the 'export.conf' file.
configuration:
All export options are defined in 'export.conf'. Multiple svg files can be configured using sections (see options and example below).
options:
inkscape: absolute path to the Inkscape binary (on Mac OS it's something like /somepath/Inkscape.app/Contents/Resources/bin/inkscape)
svg: the svg file path relative to 'export.conf'
dpi: the dots per inch to be used
output: where to export the images. The path is relative to 'export.conf'
id: list of inkscape object ids to export. The first line must be empty. Other lines must begin with a whitespace (or tab)
example export.conf:
[DEFAULT]
inkscape=/somepath/Inkscape.app/Contents/Resources/bin/inkscape
[exportconfig]
svg=my_awesome_draws.svg
dpi=180
output=sprites/somedir
id=
object1
object2
object3
[otherexport]
svg=other_awesome_draws.svg
dpi=90
output=sprites/otherdir
id=
object1
object2
"""
import sys,os
import argparse
import subprocess
import ConfigParser
configfile = 'export.conf'
class ExportSVG(object):
config = None
proc = None
def __init__(self,config):
self.config = config
# multi-line configuration-values with ConfigParser needs a workaround
# as seen in http://stackoverflow.com/questions/335695/lists-in-configparser
def getlist(self,section,option):
value = self.config.get(section,option)
return list(filter(None, (x.strip() for x in value.splitlines())))
def export(self):
inkscape = self.config.get('DEFAULT','inkscape')
env = os.environ.copy()
env["DISPLAY"] = ''
# Open the inkscape process
cmd = [inkscape,'--without-gui','--shell']
self.proc = subprocess.Popen(cmd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,env=env)
# Process all sections
for section in self.config.sections():
self.exportSection(section)
self.proc.stdin.close()
# Print the process output
while self.proc.poll() is None:
output = self.proc.stdout.readline()
print output,
self.proc.stdout.close()
self.proc.wait()
def exportSection(self,section):
# Get options from the config file
ids = self.getlist(section,'id')
dpi = self.config.get(section,'dpi')
svg = self.config.get(section,'svg')
path = ''
try:
path = self.config.get(section,'output')
if not os.path.exists(path):
os.makedirs(path)
except ConfigParser.NoOptionError:
pass
# When used in a loop the first call to subprocess.communicate closes the pipe
# So we have to use stdin.write. See https://gist.github.com/waylan/2353749
for oid in ids:
cmdline = '{0} --export-id-only --export-id={1} --export-png={2}.png --export-dpi={3}\n'.format(svg,oid,os.path.join(path,oid),dpi)
try:
self.proc.stdin.write(cmdline)
except IOError as e:
if e.errno == errno.EPIPE or e.errno == errno.EINVAL:
break
else:
raise
def main(argv=None):
argv = (argv or sys.argv)[1:]
parser = argparse.ArgumentParser(usage=("%(prog)s path"))
parser.add_argument("path",
type=unicode,
help="Directory containing the export.conf file")
options, args = parser.parse_known_args(argv)
if not os.path.isdir(options.path):
parser.error("Directory not found: '{0}'".format(options.path))
os.chdir(options.path)
# Open the configuration file
config = ConfigParser.ConfigParser()
config.readfp(open(configfile))
ExportSVG(config).export()
# Main
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment