Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drawing svg in python with paths not shapes or convert them

Tags:

python

svg

I'm making a microscope filter generator, first it draws svg image then they are converted in 3D for 3d printing.

I used 'svgwrite'

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.

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

Or is there a way to convert those svg shape to svg path?

example of my current svg with shape :

<?xml version="1.0" encoding="utf-8" ?>
<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">
<defs />
<circle cx="270" cy="270" fill="white" r="135.0" stroke="black" stroke-width="10" />
<circle cx="270" cy="270" r="25.0" />
<line stroke="black" stroke-width="10" x1="270" x2="270" y1="270" y2="135.0" />
<line stroke="black" stroke-width="10" x1="270" x2="405.0" y1="270" y2="347.9423" />
<line stroke="black" stroke-width="10" x1="270" x2="135.0" y1="270" y2="347.9423" />
</svg>

Thanks.

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

like image 427
sliders_alpha Avatar asked Aug 26 '16 11:08

sliders_alpha


People also ask

Is it possible to draw any path in SVG?

The element in SVG is the ultimate drawing element. It can draw anything! I've heard that under the hood all the other drawing elements ultimately use path anyway. The path element takes a single attribute to describe what it draws: the d attribute.

What is path in SVG file?

The <path> element is the most powerful element in the SVG library of basic shapes. It can be used to create lines, curves, arcs, and more. Paths create complex shapes by combining multiple straight lines or curved lines. Complex shapes composed only of straight lines can be created as <polyline> s.

How do you draw a curve in SVG?

An SVG quadratic curve will probably suffice. To draw it, you need the end points (which you have) and a control point which will determine the curve. To make a symmetrical curve, the control point needs to be on the perpendicular bisector of the line between the end points. A little maths will find it.


2 Answers

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 - http://tutorials.jenkov.com/svg/path-element.html

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

It is available using "pip install svgpathtools" (see manual there) — https://pypi.python.org/pypi/svgpathtools/

So, you may initially create high-level objects, like

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

And then just make a high-level Path object from them

path = Path(*segemnts)  # segments are decribed above - Line(), Arc(), etc

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.

In addition, your referred svgwrite lib also already provides a way to build XML representation

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

Probably even the svgpathtools itself has it (I hasn't figured all it advantages yet)

Ask me in comment, please, if something is still unanswered.

like image 92
Nikolay Prokopyev Avatar answered Oct 20 '22 18:10

Nikolay Prokopyev


Line and Circle have a straightforward translation to a Path entity, using MoveTo/LineTo/EllipticalArc.

It shouldn't be a great deal to just replace those lines in the Xml source and keep all the rest, with a home-made script.

like image 44
Yves Daoust Avatar answered Oct 20 '22 18:10

Yves Daoust