Skip to content

Instantly share code, notes, and snippets.

@sneetsher
Created January 20, 2015 16:10
Show Gist options
  • Save sneetsher/19bbfdb60788f302025a to your computer and use it in GitHub Desktop.
Save sneetsher/19bbfdb60788f302025a to your computer and use it in GitHub Desktop.
SVG is an XML, you can use this python script to generate slices. Actually, it does not slice it, but uses same copy of input file with different values of viewBox to show only target area.
#!/usr/bin/env python
"""
Generate slices
python svg_slice.py 2 1 askUbuntu.svg
ie: 2x1 grid
Then use Inkscape to export PDF for each slice/cell
for i in $(ls cell_*); do inkscape $i --export-pdf=$i.pdf; done;
"""
import sys, os.path
import xml.etree.ElementTree as ET
fin=sys.argv[-1]
hsplit=int(sys.argv[1])
vsplit=int(sys.argv[2])
tree = ET.parse(fin)
root = tree.getroot()
hstep=float(root.attrib['width'])/hsplit
vstep=float(root.attrib['height'])/vsplit
root.attrib['width']=str(hstep)
root.attrib['height']=str(vstep)
for i in range(hsplit):
for j in range(vsplit):
root.attrib['viewBox']='%.4f %.4f %.4f %.4f' % (i*hstep, j*vstep, hstep, vstep)
tree.write('cell_%i-%i_%s' % (i,j,os.path.basename(fin)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment