drawing svg in python with paths not shapes or convert them
<p>I'm making a microscope filter generator, first it draws svg image then they are converted in 3D for 3d printing.</p>

<p>I used 'svgwrite'</p>

<p>However this librayry generates svg with shapes (line, circle, etc), at the time I didn't know but every 3D conversion librayry/softwares needs the svg to contain path.</p>

<p>Is there a librayry that generates svg files with path (but allow me in the script to draw easily circles, lines, etc?) </p>

<p>Or is there a way to convert those svg shape to svg path?</p>

<p>example of my current svg  with shape :</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
&lt;svg baseProfile="tiny" height="100%" version="1.2" width="100%" xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xlink="http://www.w3.org/1999/xlink"&gt;
&lt;defs /&gt;
&lt;circle cx="270" cy="270" fill="white" r="135.0" stroke="black" stroke-width="10" /&gt;
&lt;circle cx="270" cy="270" r="25.0" /&gt;
&lt;line stroke="black" stroke-width="10" x1="270" x2="270" y1="270" y2="135.0" /&gt;
&lt;line stroke="black" stroke-width="10" x1="270" x2="405.0" y1="270" y2="347.9423" /&gt;
&lt;line stroke="black" stroke-width="10" x1="270" x2="135.0" y1="270" y2="347.9423" /&gt;
&lt;/svg&gt;
</code></pre>

<p>Thanks.</p>

<p>PS : note that I have to do this programaticaly because I intend to generate a lot of filters.</p>

<p>I have already written some stuff for my own needs to process some similar tasks with SVG elements, like evaluating of bounding boxes, transformations and so on. Thus, this task seems relatively simple for me to implement such a conversion. All you need for it is only knowledge of what paths "d" attribute consists from - there is actually a list of lines, eliptical arcs and bezier curves (you even do not need the most complicated latters). See this useful tutorial if you're interested in customization of this - <a href="http://tutorials.jenkov.com/svg/path-element.html" rel="nofollow">http://tutorials.jenkov.com/svg/path-element.html</a></p>

<p>But when I had started to answering you, I found a recent ready-for-use library which seems perfectly fit on you needs. </p>

<p>It is available using "pip install svgpathtools" (see manual there) â <a href="https://pypi.python.org/pypi/svgpathtools/" rel="nofollow">https://pypi.python.org/pypi/svgpathtools/</a></p>

<p>So, you may initially create high-level objects, like</p>

<pre><code>Line(start, end)
Arc(start, radius, rotation, large_arc, sweep, end) 
# See docstring for a detailed explanation of these parameters, 
# but you're definetely able to create cirlces that way
</code></pre>

<p>And then just make a high-level Path object from them</p>

<pre><code>path = Path(*segemnts)  # segments are decribed above - Line(), Arc(), etc
</code></pre>

<p>Now you are able to get path.d() string and just build an XML representation using your desired attributes (stroke, stroke-width, etc), since the main svg-path data is stored exactly in "d" attribute, which value you are already have.</p>

<p>In addition, your referred <em>svgwrite</em> lib also already provides a way to build XML representation</p>

<pre><code>svgwrite.path.Path(d=path.d(), stroke='black', **extra)  
# **extra is every other common SVG attribute as keyword arguments
</code></pre>

<p>Probably even the <em>svgpathtools</em> itself has it (I hasn't figured all it advantages yet)</p>

<p>Ask me in comment, please, if something is still unanswered.</p>

python svg