Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
PNG file format supports lossless image compression that makes it popular among its users. Unlike SVG, a vector format that all platforms and applications may not support, PNG is universal and can be easily used across various software, websites, and devices. Additionally, PNG supports transparent backgrounds, making it ideal for web graphics, logos, and images. With Aspose.HTML for Python via .NET, you can convert SVG to PNG format programmatically with full control over a wide range of conversion parameters.
In this article, you will find information on SVG to PNG conversion by using convert_svg() methods of the Converter class and applying ImageSaveOptions. Also, you can try an Online SVG Converter to test the Python API functionality and convert SVG on the fly.
You can convert SVG to other formats in real time. Load SVG from a local file system or URL, select the output format and run the example. The save options are set by default. You will immediately receive the conversion result as a separate file.
Using convert_svg() methods is the most common way to convert SVG into various formats. To convert, you can load SVG from a file, URL, or code string. In the following example, we create an SVG file from code.
format property is PNG. If you do not set specific options for saving the resulting image, the default options will be used.content, base_uri, options, output_path) method of the Converter class. 1# Convert SVG to PNG using Python
2
3import os
4import aspose.html.converters as conv
5import aspose.html.saving as sav
6
7# Setup directories and define paths
8output_dir = "output/"
9if not os.path.exists(output_dir):
10 os.makedirs(output_dir)
11save_path = os.path.join(output_dir, "circle.png")
12
13# Prepare SVG code
14svg_code = """<svg xmlns="http://www.w3.org/2000/svg">
15<circle cx="100" cy="100" r="60" fill="teal" stroke="salmon" stroke-width="10" />
16</svg>"""
17
18# Initialize ImageSaveOptions
19options = sav.ImageSaveOptions()
20
21# Convert SVG to PNG
22conv.Converter.convert_svg(svg_code, ".", options, save_path)The PNG images creation functionality can be enhanced with save options per your needs. The
ImageSaveOptions class offers extensive customization for converting SVG content into image formats. Here is a description of each property of ImageSaveOptions:
CssOptions, allows configuring how CSS properties are processed during the HTML to image conversion.To convert SVG to PNG with ImageSaveOptions specifying, you should follow a few steps:
format property is PNG. The ImageSaveOptions() constructor initializes an instance of the ImageSaveOptions class that is passed to convert_svg() method. Here, you can set the required save options, such as format or resolution.document, options, and output file path save_path and performs the conversion operation. 1# Convert SVG to PNG with custom settings using Python
2
3import os
4import aspose.html.dom.svg as ahsvg
5import aspose.html.converters as conv
6import aspose.html.saving as sav
7import aspose.html.drawing as dr
8
9# Setup directories and define paths
10output_dir = "output/"
11input_dir = "data/"
12if not os.path.exists(output_dir):
13 os.makedirs(output_dir)
14
15document_path = os.path.join(input_dir, "tulips.svg")
16save_path = os.path.join(output_dir, "tulips.png")
17
18# Load an SVG document
19document = ahsvg.SVGDocument(document_path)
20
21# Initialize ImageSaveOptions
22options = sav.ImageSaveOptions()
23options.page_setup.first_page = dr.Page(dr.Size(500, 500), dr.Margin(10, 10, 10, 10))
24options.css.media_type.PRINT
25
26# Convert SVG to PNG
27conv.Converter.convert_svg(document, options, save_path)In the above example, we use:
page_setup property to set the layout settings for the first page of the output image;css.media_type property to specify the media type to be used for CSS during the rendering process. In the example, it is set to PRINT, which applies print-specific CSS rules. This can affect the final appearance of the output image by using styles defined for print media.The figure shows the quality of SVG to PNG rendering using a fragment of the tulips.png file as an example.

Aspose.HTML for Python via .NET supports converting SVG to PNG, JPEG, BMP, TIFF, and GIF images. To set the output image format, you only need to specify the required extension (format) in the output file name and set the format property for the save options object.
For example, to convert SVG to GIF, you need:
options = sav.ImageSaveOptions(rim.ImageFormat.GIF).gif in the output image file name: save_path = os.path.join(output_dir, "svg-to-image.gif")Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.