Convert SVG to PNG

Convert SVG to PNG

Aspose.Imaging for Python via .NET provides the SvgImage class to load SVG files and same can be used to save the image to PNG format.

Below provided sample code demonstrate how to convert SVG to PNG.

import aspose.pycore as aspycore
from aspose.imaging import Image
from aspose.imaging.imageoptions import PngOptions, SvgRasterizationOptions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
with Image.load(os.path.join(data_dir, "template.svg")) as image:
options = PngOptions()
svg_options = SvgRasterizationOptions()
svg_options.page_width = 100.0
svg_options.page_height = 200.0
options.vector_rasterization_options = svg_options
image.save(os.path.join(data_dir, "result.png"), options)
if delete_output:
os.remove(os.path.join(data_dir, "result.png"))