Convert SVG to PNG – Python Examples

In this article, you will find information on how to convert SVG to PNG using Aspose.SVG for Python via .NET. Here you will find Python code examples for SVG to PNG conversion with default save options and with exactly specified conversion options. The article provides a general description of the conversion features of Aspose.SVG for python via .NET and describes supported scenarios of SVG to Image conversions by using Converter class.

Online SVG Converter

You can convert SVG to images and other popular formats in any way – online or programmatically. Check the Aspose.SVG API functionality and convert SVG in real-time! Please load SVG from a local file system or URL, select the output format and run the converter. In the example, the save options are set by default. You will immediately receive the result as a separate file.

                
            

If you want to convert SVG to PNG programmatically, please see the following conversion scenarios and Python examples.

Convert SVG to PNG – Using the convert_svg() method

The PNG (Portable Network Graphics) format is a widely used image format known for its lossless compression, which means it preserves all the details of an image without losing quality during compression. It supports transparency, making it ideal for web graphics, logos, and images with transparent backgrounds. PNG is popular because it offers high image quality, excellent compression ratio, and wide compatibility with various applications and web browsers. Additionally, it supports a wide range of colors and is suitable for images that require editing and resaving.

Using convert_svg() methods is the most common way to convert SVG to various popular formats. The following code snippet shows how to convert SVG to PNG with default save options:

 1from aspose.svg import *
 2from aspose.svg.converters import *
 3from aspose.svg.drawing.skiasharp import *
 4from aspose.svg.saving import *
 5
 6# Activate the Aspose.SVG.Drawing.SkiaSharp feature
 7Configuration.set_extension(SkiaModule())
 8options = ImageSaveOptions()
 9with SVGDocument("document.svg") as document:
10
11    # Convert SVG to PNG
12    Converter.convert_svg(document, options, "result.png")

Save Options – ImageSaveOptions Class

Aspose.SVG allows converting SVG to Image file formats using default or custom save options. ImageSaveOptions usage enables you to customize the rendering process. For example, you can specify the image format, page size, margins, background color, etc.

PropertyDescription
compressionSets Tagged Image File Format (TIFF) Compression. By default, this property is LZW.
cssGets a CssOptions object which is used for configuration of CSS properties processing.
formatSets the ImageFormat (JPG, PNG, BMP, TIFF, or GIF). By default, this property is PNG.
background_colorThis property sets the color that will fill the background. By default, this property is transparent.
page_setupThis property allows you to define the layout of the page, including dimensions and margins.
horizontal_resolutionSets the horizontal resolution for output and internal images in pixels per inch (dpi). By default, this property is set to 300 dpi, which is used unless overridden by specific conditions. The resolution is always applied unless the Page size is set in pixels (px), in which case the default resolution is 96 dpi.
vertical_resolutionSets the vertical resolution for output and internal images in pixels per inch (dpi). By default, this property is set to 300 dpi, which is used unless overridden by specific conditions. The resolution is always applied unless the Page size is set in pixels (px), in which case the default resolution is 96 dpi.
smoothing_modeThis property sets the rendering quality for this image.
textGets a TextOptions object which is used for configuration of text rendering.

Note: The options that are implementing with the ImageSaveOptions class are inheriting from the ImageRenderingOptions class.

Convert SVG to PNG using save options

The following Python example shows how to use ImageSaveOptions and convert SVG to PNG with custom save options. In addition, the following Python example will show how to configure the paths to the source and output files in your file system.

Note: We recommend that you activate the Aspose.SVG.Drawing.SkiaSharp function. The SkiaSharp module is a graphics library used to render SVG content. This ensures that the rendering engine supports the operations required for the conversion and provides the best result.

 1import os
 2import aspose
 3from aspose.svg import *
 4from aspose.svg.converters import *
 5from aspose.svg.drawing.skiasharp import *
 6from aspose.svg.rendering import *
 7from aspose.svg.drawing import *
 8from aspose.svg.saving import *
 9
10# Initialize an SVG document from a file
11input_folder = "data/"
12output_folder = "output/"
13src_file = os.path.join(input_folder, "winter.svg")
14output_file = os.path.join(output_folder, "winter.png")
15if not os.path.exists(output_folder):
16    os.makedirs(output_folder)
17
18# Activate the Aspose.SVG.Drawing.SkiaSharp feature
19Configuration.set_extension(SkiaModule())
20options = ImageSaveOptions()
21options.background_color = aspose.pydrawing.Color.from_argb(231, 217, 230)
22options.page_setup.any_page = Page(Size(600, 550), Margin(10, 10, 10, 10))
23options.horizontal_resolution = Resolution.from_dots_per_inch(96.0)
24options.vertical_resolution = Resolution.from_dots_per_inch(96.0)
25with SVGDocument(src_file) as document:
26
27    # Convert SVG to PNG
28    Converter.convert_svg(document, options, output_file)

You can evaluate the quality of conversion by trying our product. Here, we provide an illustration - the following Figure shows the original winter.svg image (a) and the converted to PNG the winter.png image with a new background color (b):

Text “The original winter.svg image and the converted to PNG the winter.png image with a new background color”

You can try our free online SVG to PNG Converter that works with high quality, easy and fast. Just upload SVG, convert it, and get results in seconds! It’s fast, easy, and completely free!

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.