CairoSVG is a powerful tool that can convert images in SVG1.1 format to common formats such as PNG, PDF, and PS. Here is a detailed tutorial on using CairoSVG:
1. Introduction to CairoSVG
- definitionCairoSVG is a Python-based library that uses the Cairo graphics library to draw SVG images and supports converting SVG images to multiple formats.
- Supported version: CairoSVG supports Python 3.5 and above (some information indicates that at least Python 3.6 is required). The older CairoSVG() version is Python compatible, but is no longer supported.
- Dependencies: CairoSVG relies on tinycss2 and csselect2 to apply CSS styles, rely on defusedxml to detect unsafe SVG files, and embedded raster images are processed by the Pillow library.
2. Install CairoSVG
You can use the pip command to install CairoSVG:
pip3 install cairosvg
Please note that additional tools may be required during the installation process, and the names of these tools depend on the operating system you are using. For example, on Windows, you need to install the Visual C++ compiler for Python and Cairo; on macOS, you need to install cairo and libffi; on Linux, you need to install cairo, python3-dev, and libffi-dev.
3. Use CairoSVG
1. Command line usage
With the command line, you can easily convert SVG files using CairoSVG. For example, to convert files in the current directory into files, you can use the following command:
cairosvg -o
The detailed description of CairoSVG command line parameters is as follows:
-
-h
or--help
: Show help information and exit. -
-v
or--version
: Display the version number of the program and exit. -
-f
or--format
: Specify the output format, supports pdf, png, ps and svg. -
-d
or--dpi
: Set the DPI ratio, that is, the ratio between 1 inch and 1 pixel. -
-W
or--width
: Sets the width of the parent container in pixels. -
-H
or--height
: Set the height of the parent container in pixels. -
-s
or--scale
: Set the output scaling factor. -
-u
or--unsafe
: parsing XML entities and allowing very large files (Warning: vulnerable to XXE attacks and various DoS attacks). -
--output-width
and--output-height
: Set the desired output width and height in pixels, respectively. -
-o
or--output
: Specify the output file name.
2. Python library usage
CairoSVG also provides a module for Python, which provides four functions: svg2pdf, svg2png, svg2ps and svg2svg (some materials also mention the svg2gif function, but please note that not all versions support it). These functions are used as follows:
- svg2png: Convert SVG images to PNG format.
import cairosvg cairosvg.svg2png(url="/path/to/", write_to="/tmp/")
- svg2pdf: Convert SVG images to PDF format.
cairosvg.svg2pdf(file_obj=open("/path/to/", "rb"), write_to="/tmp/")
- svg2ps: Convert SVG images to PS format.
output = cairosvg.svg2ps(bytestring=open("/path/to/").read().encode('utf-8'))
- svg2svg: Cut or other processing of SVG files (if supported).
These functions require one of the following named parameters: url (a URL or file name), file_obj (a file class object), or bytestring (a byte string containing SVG). They can also receive optional parameters corresponding to command line options, such as parent_width, parent_height, dpi, scale, etc. If the write_to parameter (filename or file-like object) is provided, the output is written here; otherwise, the function returns a byte string.
4. Things to note
- CairoSVG usually supports common features of the SVG specification well, but is not good at handling unstable SVG files with unknown syntax or unavailable external resources.
- Slight image distortion issues may occur when using CairoSVG for image conversion. This is usually due to inherent losses when a vector image is converted to a raster image.
- Be careful to protect your SVG files from potential XXE and DoS attacks. When using
--unsafe
Be sure to exercise caution when choosing.
With the above tutorial, you should be able to easily convert SVG images to other formats using CairoSVG. Whether in the command line interface or in Python programs, CairoSVG provides convenient and powerful features to meet your needs.
This is the end of this article about using the CairoSVG library in Python. For more related content of the Python CairoSVG library, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!