Convert SVG to PNG in Python

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.

Online SVG Converter

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.

                
            

Convert SVG to PNG

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.

  1. Prepare code for an SVG document.
  2. Create a new ImageSaveOptions object. By default, the format property is PNG. If you do not set specific options for saving the resulting image, the default options will be used.
  3. Use the convert_svg(content, base_uri, options, output_path) method of the Converter class.
 1import os
 2from aspose.html.converters import *
 3from aspose.html.saving import *
 4
 5# Setup directories and define paths
 6output_dir = "output/"
 7if not os.path.exists(output_dir):
 8    os.makedirs(output_dir)
 9save_path = os.path.join(output_dir, "circle.png")
10
11# Prepare SVG code
12svg_code = """<svg xmlns="http://www.w3.org/2000/svg">
13<circle cx="100" cy="100" r="60" fill="teal" stroke="salmon" stroke-width="10" />
14</svg>"""
15
16# Initialize ImageSaveOptions
17options = ImageSaveOptions()
18
19# Convert SVG to PNG
20Converter.convert_svg(svg_code, ".", options, save_path)

Convert SVG to PNG using ImageSaveOptions

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:

To convert SVG to PNG with ImageSaveOptions specifying, you should follow a few steps:

  1. Load an SVG file using one of the SVGDocument() constructors of the SVGDocument class. ( tulips.svg).
  2. Create a new ImageSaveOptions object and specify save options. By default, the 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.
  3. Use one of the convert_svg() methods to save SVG as a PNG image. In the example, the convert_svg() method takes the document, options, and output file path save_path and performs the conversion operation.
 1import os
 2from aspose.html.saving import *
 3# from aspose.html.drawing import *
 4from aspose.html.converters import *
 5from aspose.html.dom.svg import *
 6
 7# Setup directories and define paths
 8output_dir = "output/"
 9input_dir = "data/"
10if not os.path.exists(output_dir):
11    os.makedirs(output_dir)
12
13document_path = os.path.join(input_dir, "tulips.svg")
14save_path = os.path.join(output_dir, "tulips.png")
15
16# Load an SVG document
17document = SVGDocument(document_path)
18
19# Настройки сохранения в PDF
20options = ImageSaveOptions()
21options.page_setup.first_page = Page(Size(500, 500), Margin(10, 10, 10, 10))
22options.css.media_type.PRINT
23
24# Convert SVG to PNG
25Converter.convert_svg(document, options, save_path)

In the above example, we use:

The figure shows the quality of SVG to PNG rendering using a fragment of the tulips.png file as an example.

Text “tulips.png image”

How to Convert SVG to Images

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:

See Also

Text “SVG to PNG Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.