import cairo import rsvg import xml.etree.ElementTree as ET def convertSVGToPNG(src, dst, dstWidth, dstHeight, srcWidth, srcHeight, style,layerID): ET.register_namespace("","http://www.w3.org/2000/svg") tree = ET.parse(src) root = tree.getroot() for layer in root.findall('./{http://www.w3.org/2000/svg}g'): name = layer.get('{http://www.inkscape.org/namespaces/inkscape}label') if name in layerID : for path in layer.findall('./{http://www.w3.org/2000/svg}path'): path.set("style",style) else : root.remove(layer) img = cairo.ImageSurface(cairo.FORMAT_ARGB32, dstWidth, dstHeight) ctx = cairo.Context(img) width_ratio = float(dstWidth) / float(srcWidth) height_ratio = float(dstHeight) / float(srcHeight) ctx.scale(width_ratio, height_ratio) handler= rsvg.Handle(None,str(ET.tostring(root, encoding='utf8', method='xml'))) handler.render_cairo(ctx) img.write_to_png(dst) def convertSVGToPNG2(src, dst, dstWidth, dstHeight, srcWidth, srcHeight, style,layerID): ET.register_namespace("","http://www.w3.org/2000/svg") tree = ET.parse(src) root = tree.getroot() for path in root.findall('./{http://www.w3.org/2000/svg}path'): path.set("style",style) img = cairo.ImageSurface(cairo.FORMAT_ARGB32, dstWidth, dstHeight) ctx = cairo.Context(img) width_ratio = float(dstWidth) / float(srcWidth) height_ratio = float(dstHeight) / float(srcHeight) ctx.scale(width_ratio, height_ratio) handler= rsvg.Handle(None,str(ET.tostring(root, encoding='utf8', method='xml'))) handler.render_cairo(ctx) img.write_to_png(dst)