{"id":6902,"library":"svgelements","title":"SVG Elements Parsing","description":"svgelements is a high-fidelity Python library for parsing and geometrically rendering SVG (Scalable Vector Graphics) files. It aims to correctly process SVG for use as geometric data, providing robust representations for core SVG elements such as Path, Matrix, Angle, Length, Color, and Point. The library is compatible with Python 3+ and adheres to SVG standard 1.1 and elements of 2.0, originating from the MeerK40t laser cutting project. It is currently at version 1.9.6 and sees active maintenance with frequent patch releases.","status":"active","version":"1.9.6","language":"en","source_language":"en","source_url":"https://github.com/meerk40t/svgelements","tags":["svg","parsing","graphics","vector","geometry","renderer"],"install":[{"cmd":"pip install svgelements","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Optional dependency for faster and more accurate Arc.length() calculations, otherwise uses geometric approximation which can be slow.","package":"scipy","optional":true},{"reason":"Soft dependency for loading embedded images within SVG files via SVGImage objects.","package":"Pillow","optional":true}],"imports":[{"note":"The project was renamed from 'svg.elements' to 'svgelements' around version 0.7.6/1.0.0. Older import paths will fail.","wrong":"from svg.elements import SVG","symbol":"SVG","correct":"from svgelements import SVG"},{"note":"Commonly used for quick access to all core elements like SVG, Path, Matrix, Rect, etc.","symbol":"*","correct":"from svgelements import *"}],"quickstart":{"code":"from svgelements import SVG, Rect, Group\nimport io\n\n# Example 1: Parse an SVG string\nsvg_string = '''<svg width=\"100\" height=\"100\">\n  <rect x=\"10\" y=\"10\" width=\"80\" height=\"80\" fill=\"red\" />\n</svg>'''\nsvg_doc = SVG.parse(io.StringIO(svg_string))\nprint(f\"Parsed SVG root element: {svg_doc.xml_tag}\")\nfor element in svg_doc.elements():\n    if isinstance(element, Rect):\n        print(f\"  Found Rect: x={element.x}, y={element.y}, width={element.width}, height={element.height}, fill={element.fill}\")\n\n# Example 2: Create an SVG programmatically and write to a file\nnew_svg = SVG()\nnew_svg.append(Rect(x=0, y=0, width=\"2in\", height=\"2in\", fill=\"blue\", stroke=\"black\"))\nnew_svg.append(Group(id=\"my_group\").add(Rect(x=10, y=10, width=30, height=30, fill=\"green\")))\n\n# Save to a temporary file\noutput_filename = \"output.svg\"\nnew_svg.write_xml(output_filename)\nprint(f\"Generated SVG saved to {output_filename}\")\n\n# You can also get the XML as a string\nsvg_output_string = new_svg.string_xml()\nprint(\"\\nGenerated SVG string:\\n\" + svg_output_string)\n\n# Clean up the created file\nimport os\nif os.path.exists(output_filename):\n    os.remove(output_filename)\n    print(f\"Cleaned up {output_filename}\")","lang":"python","description":"This quickstart demonstrates how to parse an existing SVG string and iterate through its elements. It also shows how to programmatically create an SVG document with basic shapes (Rect, Group) and save it to a file or retrieve its XML as a string. The `SVG.parse()` method can take a file path, file-like object, or string stream. The `write_xml()` and `string_xml()` methods are available for generating SVG output."},"warnings":[{"fix":"Update import statements: `from svg.elements import X` -> `from svgelements import X`.","message":"The project was renamed from `svg.elements` to `svgelements`. Older code using `from svg.elements import ...` will no longer work and needs to be updated to `from svgelements import ...`.","severity":"breaking","affected_versions":"<1.0.0 (specifically pre-0.7.7)"},{"fix":"Install `scipy` (`pip install scipy`) for exact hypergeometric calculations where applicable, which greatly speeds up `Arc.length()`.","message":"Calculating the length of `CubicBezier` and `Arc` path segments via `Path.length()` or similar methods can be computationally intensive and slow, as it relies on geometric approximation by default. Performance can be significantly improved by installing `scipy`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Adopt `SVG.write_xml(filename)` or `SVG.string_xml()` for consistent and supported SVG output generation. Review the `1.9.0` release notes for detailed changes.","message":"The `write_xml` and `string_xml` methods were introduced around version 1.9.0, standardizing how SVG objects are serialized. While not strictly a 'breaking' change for existing code that didn't use this functionality, the internal structure for writing XML was significantly re-factored. Relying on pre-1.9.0 implicit XML generation methods may lead to unexpected behavior or missing features.","severity":"deprecated","affected_versions":"<1.9.0"},{"fix":"Ensure `Pillow` is installed if your SVG files contain embedded images: `pip install Pillow`.","message":"Loading embedded images within SVG files using `SVGImage` objects implicitly requires the `Pillow` (PIL fork) library. If `Pillow` is not installed, image loading within parsed SVGs will fail silently or raise errors related to missing image handling.","severity":"gotcha","affected_versions":"All versions"},{"fix":"When parsing SVGs, especially those with relative units or for specific display contexts, provide accurate `width`, `height`, and `ppi` (defaults to 96) values to `SVG.parse()` to ensure correct scaling and geometry interpretation.","message":"The `SVG.parse()` method accepts `width`, `height`, and `ppi` parameters. These values are crucial for correctly interpreting relative SVG units (like percentages) and converting between different unit systems, as the SVG specification itself does not directly define a 'physical' view size or pixels per inch.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}