def svg2png(svg_file, output_file, scale=1):
# Get the svg files content
svg_data = open(svg_file).read()
# Get the width / height inside of the SVG
doc = minidom.parseString(svg_data)
width = [path.getAttribute('width') for path in doc.getElementsByTagName('svg')][0]
height = [path.getAttribute('height') for path in doc.getElementsByTagName('svg')][0]
width = int(round(float(re.compile('(\d+\.*\d*)\w*').findall(width)[0])))
height = int(round(float(re.compile('(\d+\.*\d*)\w*').findall(height)[0])))
doc.unlink()
# Create the png
img = cairo.ImageSurface(
cairo.FORMAT_ARGB32, width * scale, height * scale)
ctx = cairo.Context(img)
ctx.scale(scale, scale)
handler = rsvg.Handle(None, str(svg_data))
handler.render_cairo(ctx)
img.write_to_png(output_file)
print("{} ==> {}".format(svg_file, output_file))
评论列表
文章目录