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.
 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)

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.
 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:

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.