BachiLi / diffvg

Differentiable Vector Graphics Rasterization
https://people.csail.mit.edu/tzumao/diffvg/
Apache License 2.0
902 stars 152 forks source link

saving svg fails for Circle as attributes are not str #32

Closed fjenett closed 2 years ago

fjenett commented 2 years ago

Here: https://github.com/BachiLi/diffvg/blob/fd38f71527b6258ddaf54ba2db3596b396dc80e1/pydiffvg/save_svg.py#L78

The attributes need to be passed as strings, otherwise converting the xml tree to a string in prettify() fails with:

TypeError                                 Traceback (most recent call last)
/usr/lib/python3.7/xml/etree/ElementTree.py in _escape_attrib(text)
   1079     try:
-> 1080         if "&" in text:
   1081             text = text.replace("&", "&")
TypeError: argument of type 'float' is not iterable

Here is the fix:

shape_node.set('r', str(shape.radius.item()))
shape_node.set('cx', str(shape.center[0].item()))
shape_node.set('cy', str(shape.center[1].item()))
robinsloan commented 2 years ago

@fjenett Just a note to say thanks for this—I'm not sure I would have figured out the problem otherwise. I successfully patched this in a fork & it's working great for me. Thanks again!

BachiLi commented 2 years ago

FYI, I welcome PR for fixing this.

robinsloan commented 2 years ago

@BachiLi For sure! Just submitted one.

BachiLi commented 2 years ago

thanks!