Python SVG parser

前端 未结 3 2047
北恋
北恋 2020-12-14 08:32

I want to parse an SVG file using python to extract coordinates/paths (I believe this is listed under the \"path\" ID, specifically the d=\"...\"/>). This data will eventua

3条回答
  •  渐次进展
    2020-12-14 08:42

    The question was about extracting the path strings, but in the end the line drawing commands were wanted. Based on the answer with minidom, I added the path parsing with svg.path to generate the line drawing coordinates:

    #!/usr/bin/python3
    # requires svg.path, install it like this: pip3 install svg.path
    
    # converts a list of path elements of a SVG file to simple line drawing commands
    from svg.path import parse_path
    from svg.path.path import Line
    from xml.dom import minidom
    
    # read the SVG file
    doc = minidom.parse('test.svg')
    path_strings = [path.getAttribute('d') for path
                    in doc.getElementsByTagName('path')]
    doc.unlink()
    
    # print the line draw commands
    for path_string in path_strings:
        path = parse_path(path_string)
        for e in path:
            if isinstance(e, Line):
                x0 = e.start.real
                y0 = e.start.imag
                x1 = e.end.real
                y1 = e.end.imag
                print("(%.2f, %.2f) - (%.2f, %.2f)" % (x0, y0, x1, y1))
    

提交回复
热议问题