Convert SVG to PNG in Python

To convert SVG to PNG in Python, load the source file with SVGDocument, create ImageSaveOptions, and pass both objects to Converter.convert_svg(). PNG is the default image output format, and ImageSaveOptions lets you control the background color, page size, margins, and resolution.

Aspose.SVG APIs Used

APIPurpose
SVGDocumentLoads the source SVG document
Converter.convert_svg()Converts and saves the SVG document as PNG
ImageSaveOptionsConfigures image rendering and output settings
Page and ResolutionSet the output page and raster resolution

Convert SVG to PNG in Python

PNG is a lossless raster format that supports transparency. It is a practical output format for web graphics, previews, thumbnails, and applications that cannot render SVG directly.

The following example converts document.svg to result.png using the default PNG settings:

1from aspose.svg import SVGDocument
2from aspose.svg.converters import Converter
3from aspose.svg.saving import ImageSaveOptions
4
5# Convert an SVG file to PNG using the default save options
6options = ImageSaveOptions()
7
8with SVGDocument("document.svg") as document:
9    Converter.convert_svg(document, options, "result.png")

The code performs three operations:

  1. SVGDocument loads the SVG file and resolves its content and linked resources.
  2. ImageSaveOptions selects image output; its default format is PNG.
  3. Converter.convert_svg() renders the document and writes result.png.

Convert SVG to PNG with Custom Options

Use ImageSaveOptions when the PNG needs a specific canvas, background, or resolution. The following example converts winter.svg, adds a solid background, creates a 600 × 550 page with 10-pixel margins, and renders at 96 dpi:

 1import os
 2from aspose.svg import SVGDocument
 3from aspose.svg.converters import Converter
 4from aspose.svg.saving import ImageSaveOptions
 5from aspose.svg.drawing import Resolution, Page, Size, Margin
 6from aspose.pydrawing import Color
 7
 8# Convert SVG to PNG with a custom background, page, and resolution
 9input_folder = "data/"
10output_folder = "output/"
11input_path = os.path.join(input_folder, "winter.svg")
12output_path = os.path.join(output_folder, "winter.png")
13os.makedirs(output_folder, exist_ok=True)
14
15options = ImageSaveOptions()
16options.background_color = Color.from_argb(231, 217, 230)
17options.page_setup.any_page = Page(Size(600, 550), Margin(10, 10, 10, 10))
18options.horizontal_resolution = Resolution.from_dots_per_inch(96.0)
19options.vertical_resolution = Resolution.from_dots_per_inch(96.0)
20
21with SVGDocument(input_path) as document:
22    Converter.convert_svg(document, options, output_path)

The resulting PNG uses the requested page settings instead of relying only on the SVG viewport. The illustration compares the source SVG with the PNG rendered on the new background.

Original SVG and converted PNG with a custom background

Important ImageSaveOptions Properties

PropertyWhen to use it
background_colorFill transparent areas with a chosen color
page_setupSet page dimensions and margins
horizontal_resolutionSet horizontal output resolution in dpi
vertical_resolutionSet vertical output resolution in dpi
smoothing_modeControl rasterization quality
textConfigure text rendering through TextOptions

If background_color is not set, transparent SVG areas remain transparent in PNG. Resolution affects raster density, while page_setup controls the output canvas. When page dimensions are specified in pixels, pixel dimensions are usually more important than increasing dpi alone.

Common Mistakes and Fixes

ProblemLikely causeFix
The PNG is blank or croppedThe SVG has no usable viewport or the output page is too smallDefine SVG width, height, or viewBox, or set options.page_setup.any_page
Transparent areas appear unexpectedNo output background was specifiedSet options.background_color when a solid canvas is required
Text looks differentA referenced font is unavailableInstall the font or vectorize text before conversion when exact glyph appearance is required
Linked images or CSS are missingRelative resources cannot be resolvedLoad the SVG from its file path or provide a correct base URI
Output dimensions are not as expectedPage size and resolution are being treated as the same settingUse page_setup for dimensions and horizontal_resolution/vertical_resolution for dpi

Online SVG to PNG Converter

You can also test SVG to PNG conversion in a browser. Upload an SVG file, select PNG, and compare the result with your programmatic output.

                
            

Try the free online SVG to PNG Converter to convert an SVG file without writing code.

FAQ

What is the shortest way to convert SVG to PNG in Python?

Create ImageSaveOptions(), open the source with SVGDocument, and call Converter.convert_svg(document, options, output_path).

Does SVG to PNG conversion preserve transparency?

Yes. PNG supports transparency, and transparent SVG areas remain transparent unless you set ImageSaveOptions.background_color.

How do I set the PNG size?

Set ImageSaveOptions.page_setup.any_page with a Page, Size, and Margin. Use the resolution properties separately when you need a particular dpi.

Can I convert SVG content stored in a string?

Yes. Create an SVGDocument from the SVG string and a base URI, then pass that document to Converter.convert_svg().

Related Articles