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:
- Use the
set_extension() method of the
Configuration
class to register theSkiaSharp
extension. TheSkiaSharp
module is a graphics library used for rendering SVG content. It ensures that the rendering engine supports the operations needed for the conversion. - Create an instance of the ImageSaveOptions class.
- Open a source SVG document using the SVGDocument class.
- Use the convert_svg() method to convert and save SVG as a PNG file.
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.
Property | Description |
---|---|
compression | Sets Tagged Image File Format (TIFF) Compression. By default, this property is LZW. |
css | Gets a CssOptions object which is used for configuration of CSS properties processing. |
format | Sets the ImageFormat (JPG, PNG, BMP, TIFF, or GIF). By default, this property is PNG. |
background_color | This property sets the color that will fill the background. By default, this property is transparent. |
page_setup | This property allows you to define the layout of the page, including dimensions and margins. |
horizontal_resolution | Sets 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_resolution | Sets 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_mode | This property sets the rendering quality for this image. |
text | Gets 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.
- Use the set_extension() method of the Configuration class to register the SkiaSharp extension.
- Create an instance of the
ImageSaveOptions class and specify required save options:
- Use the
background_color
property to set the color that will fill the background. - Use the
page_setup
property to set page size amd margins. - Use the
horizontal_resolution
andvertical_resolution
properties to set the horizontal and vertical resolutions for output image.
- Use the
- Open a source SVG document using the SVGDocument class.
- Use the convert_svg() method to convert and save SVG as a PNG file.
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):
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!