←back to thread

An SVG is all you need

(jon.recoil.org)
258 points sadiq | 9 comments | | HN request time: 0.402s | source | bottom
1. amelius ◴[] No.46238590[source]
I recently found out that it is surprisingly hard to convert an SVG file that consists of series of line segments into a list of those line segments in Python.

I tried with ChatGPT and Claude but both were not able to find a solution that respects the entire specification, especially transforms.

Initially, my expectation was that there must be a library for this kind of thing, but alas.

replies(5): >>46238679 #>>46238799 #>>46238853 #>>46238966 #>>46239444 #
2. nish__ ◴[] No.46238679[source]
Can't you do it by hand pretty easily? It's a list of coordinates separated by spaces. For example: "100,100 100,200 200,200 200,100"
replies(1): >>46238783 #
3. amelius ◴[] No.46238783[source]
No, the specification is more complicated, for example elements (and sub-elements recursively) can have transforms applied to them.
replies(1): >>46238900 #
4. e12e ◴[] No.46238799[source]
I mean - it's XML so you could go that way and extract the d element from path element?

But there seems to be a lot of SVG specific tooling and code to do this in python?

Eg: https://github.com/RaubCamaioni/svgpath

replies(1): >>46238941 #
5. boothby ◴[] No.46238853[source]
I find svg.path to be good for parsing path data

  https://pypi.org/project/svg.path/
For actually parsing the file, there are a number of options (in the end, it's an XML file and I tend to treat it as such)
6. boothby ◴[] No.46238900{3}[source]
Excellent point. Inkscape has (had?) a feature to simplify SVG files, which pushed transformations down the tree. I never needed to use this in an automated process, just the occasional file.
7. ◴[] No.46238941[source]
8. nawgz ◴[] No.46238966[source]
I seem to remember that the DOM nodes themselves expose some pretty useful functions. I think it was in the context of detecting edge crossings for a graph router, but you were able to interact with the computed/rendered coordinates in this context.

Sorry that's not more useful and explicit, it was a while back and never went anywhere.

9. nicoburns ◴[] No.46239444[source]
Not sure about python, but https://docs.rs/usvg in Rust is pretty good