A work-in-progress fork of the vector graphics converter CairoSVG. It is intended to be a more modular version of the library, allowing its drawing functionality to be used directly from a script in addition to its original purpose of parsing and converting SVG files.
Other branches and related repositories:
- CairoSVG/master: for proposed changes to the original library
- Cairopath: my previous attempt at a vector graphics module
- A class for each element, with methods for modifying attributes, adding child nodes and navigating the XML tree.
- Currently supported elements: structures (svg, defs, g, use), shapes (circle, ellipse, line, path, polygon, polyline, rect), clipPath.
- Helper methods and classes for complex attributes like
d
(path data) andtransform
. - Support for custom and redefined namespaces on the root element.
- Basic SVG file reading, output to SVG and formats supported by Cairo and OpenCV (PDF, PostScript, PNG, other image formats), and displaying the image in a popup window using OpenCV.
- A new name to avoid conflicts with the original library (suggestions are welcome!)
- Text nodes and elements, mask (included but not yet displaying correctly), gradients, markers, animation.
- CSS and
style
attributes. - Custom shapes like regular polygons and stars, saved as a
<path/>
with custom namespace attributes (like e.g. Inkscape does). - Not needing to have the whole tree in memory for files from disk.
See requirements.txt
for the necessary Python libraries. In addition, cairocffi needs a separate DLL file for Cairo itself, which must be named libcairo-2.dll
and placed in a folder that is in the system's PATH
.
Getting a working Cairo DLL can turn out to be a challenge, especially on Windows. The cairocffi documentation recommends installing GTK+, which includes Cairo. This worked for me when I first started working with it in 2018, but loading the DLL later failed in a new Anaconda environment on another device. I eventually found the cairo-windows repository, which has ZIP files with prebuilt 32- and 64-bit DLLs, and moved and renamed the DLL from the newest version (1.17.2) so cairocffi could find it.
import math
from cairosvg import SVG
# Create an SVG object
svg = SVG(width=600, height=600)
# ...or load a file using
# svg = SVG.read('filename.svg')
# Add elements
svg.rect(width=600, height=600, fill='#fff')
g = svg.g()
g.transform.translate(300, 300)
g.circle(r=120, fill='#000')
# Draw a five-pointed star
path = g.path(fill='#fc0')
path.M(0, -100)
for i in range(1, 5):
path.L(100*math.sin(i*4*math.pi/5), -100*math.cos(i*4*math.pi/5))
path.z()
# Display and save the image
svg.show()
svg.export('filename.svg')
svg.export('filename.png')