List of libraries for Python to manipulate SVG images
In Python, you can use several libraries to generate SVG images:
svgwrite: This is an easy-to-use Python library for generating simple SVG images. The usage is similar to drawing, you can draw lines, rectangles, circles and other shapes on the SVG canvas.
cairosvg: This is an SVG library based on the Cairo library, which can be used to convert SVG to other image formats, such as PNG, PDF, etc.
lxml: This is an xml-based library that can be used to generate and parse SVG images.
: This is a specialized library for working with SVG paths, which can be used to generate complex SVG graphics.
In the next few blogs, we will break down the above libraries for you step by step.
svgwrite library
svgwrite is a Python library for generating simple SVG images. It provides a set of drawing-like APIs that allow users to draw lines, rectangles, circles, etc. on the SVG canvas.
The library download and description address:/project/svgwrite/
The library has the following features:
- Supports a wide range of basic graphics drawing, such as lines, rectangles, circles, ellipses, arcs, polygons, etc;
- Support text drawing, you can specify the font, size, color and other styles;
- Support for combined graphics drawing, such as nested, side-by-side, overlapping, etc;
- Support style definition, you can define the color, line width, transparency and other styles for the graphics;
- Support event definition, you can define mouse click, mouse move in and other events for the graph;
- Support animation definition, you can define animation effects for graphics.
We will use svgwrite to generate an SVG image with the following steps:
Create a Drawing object and specify the filename and version of the SVG to be saved.
- Use the add() method to add graphics to the canvas.
- Use the save() method to save SVG images.
- Using the following code generates a green rectangle in the directory where the Python file is located.
import svgwrite dwg = ('ca_green.svg', profile='tiny') (((insert=(0, 0), size=("100%", "100%"), rx=None, ry=None, fill='green'))) ()
And we can see the content of the generated svg file as shown below.
<?xml version="1.0" encoding="utf-8" ?> <svg baseProfile="tiny" height="100%" version="1.2" width="100%" xmlns="http:///2000/svg" xmlns:ev="http:///2001/xml-events" xmlns:xlink="http:///1999/xlink"><defs /><rect fill="green" height="100%" width="100%" x="0" y="0" /><rect fill="green" height="100%" width="100%" x="0" y="0" /></svg>
svgwrite library other graphics drawing
line (in drawing, painting etc)
(((0, 0), (100, 100), stroke=(10, 10, 16, '%')))
orbicular
((center=(100, 100), r=40, fill='#03a9f4'))
polygonal
points = [(0, 0), (30, 30), (30, 0)] ((points=points))
These are very basic graphics that are relatively simple to use.
Reserve Anti-Crawl Techniques svgwrite Generate a Cell Phone Number
In the subsequent anti-climbing battle, we need to dynamically render the phone number, so here you need to use svg to generate the phone number effect, the code example is as follows.
import svgwrite dwg = ('phone_number.svg',size=(100,20), profile='tiny') ((insert=(0, 0), size=(100, 20), fill='red')) phone_number = '1234567890' ((phone_number, insert=(0, 15), fill='black', font_size=12)) ()
Running the code will generate a black cell phone number on the red rectangle.
To make it easier for us to encrypt numbers, we can also use the svgwrite library to randomly generate numbers between 1 and 10.
import svgwrite import random dwg = ('random_numbers.svg', size=(200, 20), profile='tiny') nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (nums) for i, num in enumerate(nums): text = ((str(num), insert=(i * 20, 20), fill='black', font_size=16)) ()
After running the effect is shown below, here must be mastered, later we have a great use.
to this article about an article to take you to understand the operation of Python and svg between the article is introduced to this, more related Python operation svg content please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!