alexandre01 / deepsvg

[NeurIPS 2020] Official code for the paper "DeepSVG: A Hierarchical Generative Network for Vector Graphics Animation". Includes a PyTorch library for deep learning with SVG data.

Home Page:https://www.lingosub.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Errors while preprocessing svg files

athena913 opened this issue · comments

Hi,

I used preprocess.py to process icons from the iconfinder dataset, which is a public site for icons. I am getting the following errors. The svg file for one of the icons from the iconfinder dataset is provided below, as an example. Does preprocess.py only work for the icons8 dataset? I would appreciate some guidance for how it can be adapted to process the svg format provided below, which does not include the "viewbox" attribute.

Thanks

svg = SVG.load_svg(svg_file)
File "../deepsvg/svglib/svg.py", line 74, in load_svg
    return SVG.from_str(f.read())
File "../deepsvg/svglib/svg.py", line 124, in from_str
    viewbox_list = list(map(float, svg_root.getAttribute("viewBox").split(" ")))
ValueError: could not convert string to float: 

<?xml version="1.0" ?><svg height="100" id="svg6584" version="1.1" width="100" xmlns="http://www.w3.org/2000/svg" xmlns:cc="http://creativecommons.org/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg"><defs id="defs6586"/><g id="layer1" transform="translate(0,-952.362)"><path d="m 45.833336,973.19536 -39.583305,22.91663 8.333327,0 0,4.16661 0,25.0001 0,4.1666 c 0,2.0834 1.858332,4.1667 4.166664,4.1667 l 12.499991,0 0,-20.8333 c 0,-2.3084 1.858291,-4.1667 4.166664,-4.1667 l 29.166646,0 c 2.308373,0 4.09458,1.8595 4.166663,4.1667 l 0,20.8333 12.499992,0 c 2.308331,0 4.166663,-1.8583 4.166663,-4.1667 l 0,-4.1666 0,-25.0001 0,-4.16661 8.333328,0 -39.583305,-22.91663 C 52.38554,972.16411 50.632666,971.11412 50,971.11203 c -0.63275,-0.002 -2.358248,1.03625 -4.166664,2.08333 z m -10.416659,39.58334 0,4.1666 29.166646,0 0,-4.1666 z m 0,8.3333 0,4.1667 29.166646,0 0,-4.1667 z m 0,8.3333 0,4.1667 29.166646,0 0,-4.1667 z" id="path4181" style="color:#000000;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:1;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate"/></g></svg>

The error occurs because the python code assumes the svg document always has a viewBox attribute.

In your case, the svg tag doesn't have a viewBox attribute, but has width and height attributes.

For now, you can manually edit the file error mentions (deepsvg/svglib/svg.py` and replace line 124:

viewbox_list = list(map(float, svg_root.getAttribute("viewBox").split(" ")))

with

        viewbox_list = None
        if svg_root.hasAttribute('viewBox'):
            viewbox = list(map(float, svg_root.getAttribute("viewBox").split(" ")))
        elif svg_root.hasAttribute('width') and svg_root.hasAttribute('height'):
            viewbox_list = [0, 0, float(svg_root.getAttribute("width")), float(svg_root.getAttribute("height"))]
        else:
            raise Exception('<svg/> does not contain width, height attributes, nor viewBox. please double check the SVG')

(hopefully the spaces are ok, but it's best to double check spaces/tab settings in your code editor to avoid Python syntax errors)