Usage¶
The module takes in utf-8 encoded svg_string and returns base64 encoded PNG string.
Lets say our svg looks like this :
We can convert it to PNG by:
import resvg_py
svg_string = """
<svg width="300" height="130" xmlns="http://www.w3.org/2000/svg">
<rect width="200" height="100" x="10" y="10" rx="20" ry="20" fill="blue" />
</svg>
"""
png_bytes: list[bytes] = resvg_py.svg_to_bytes(svg_string=svg_string) # a large list of bytes
In order to convert image to base64:
import resvg_py
import base64
svg_string = """
<svg width="300" height="130" xmlns="http://www.w3.org/2000/svg">
<rect width="200" height="100" x="10" y="10" rx="20" ry="20" fill="blue" />
</svg>
"""
# a large list of bytes
png_bytes: list[bytes] = resvg_py.svg_to_bytes(svg_string=svg_string)
base64_utf8_str = base64.b64encode(bytes(png_bytes)).decode("utf-8")
print(f"data:image/png;base64,{base64_utf8_str}")
This should return the following PNG image (check using inspect element):
We can also do something like this :
import resvg_py
svg = ... # path to svg file
print(resvg_py.svg_to_bytes(svg_path=svg))
But please do note that resvg first looks for svg_string and if that is empty then it looks for svg_path
For extra parameters refer to resvg