Convert SVG to PNG in Python – Aspose.HTML
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.
- Prepare code for an SVG document.
- 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. - 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
:
- page_setup – You can configure the page layout settings for the output image. This includes specifying the page size and margins (top, bottom, left, right) to control the placement and display of HTML content within the image.
- horizontal_resolution – This property sets or gets the horizontal resolution (in pixels per inch) for both output and internal images used during processing. Higher resolution usually results in a sharper image but can also increase file size. By default, it is 300 dpi.
- vertical_resolution – This property sets or gets the vertical resolution for internal images in pixels per inch. By default, it is 300 dpi.
- background_color – This property allows you to set the background color for the rendered output. If not set, the default background is transparent.
- css – This property, represented by
CssOptions
, allows configuring how CSS properties are processed during the HTML to image conversion. - format – This property determines the format of the output image. The supported formats include common image formats like PNG, JPEG, BMP, GIF, and TIFF. The default format is PNG, but you can specify others based on your requirements.
- smoothing_mode – This property controls the quality of graphics rendering during conversion. It affects how images are rendered, which is especially useful for anti-aliasing and achieving smooth and visually appealing output. Options typically include settings for high-quality rendering, which can be critical for professional and presentation-grade images.
- compression – The compression option allows you to set the compression method for TIFF output. Supported options: LZW, CCITT3, CCITT4, RLE, and NONE. Compression helps reduce file size while maintaining image quality, which is especially important for TIFF files used for high-quality archiving and printing of images.
- text – This property provides configurations for text rendering during HTML to image conversion.
To convert SVG to PNG with ImageSaveOptions
specifying, you should follow a few steps:
- Load an SVG file using one of the SVGDocument() constructors of the SVGDocument class. ( tulips.svg).
- Create a new
ImageSaveOptions object and specify save options. By default, the
format
property is PNG. TheImageSaveOptions()
constructor initializes an instance of theImageSaveOptions
class that is passed toconvert_svg()
method. Here, you can set the required save options, such as format or resolution. - 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 pathsave_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
page_setup
property to set the layout settings for the first page of the output image; - the
css.media_type
property to specify the media type to be used for CSS during the rendering process. In the example, it is set toPRINT
, which applies print-specific CSS rules. This can affect the final appearance of the output image by using styles defined for print media.
The figure shows the quality of SVG to PNG rendering using a fragment of the tulips.png
file as an example.
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:
- to set the format property:
options.format.GIF
- and set the extension
.gif
in the output image file name:save_path = os.path.join(output_dir, "svg-to-image.gif")
See Also
- To learn more about SVG files, visit the article What is an SVG File? – Pros, Cons, XML Code.
- Download our Aspose.HTML for Python via .NET library allows you to successfully, quickly, and easily convert your HTML, MHTML, EPUB, SVG, and Markdown documents to the most popular formats.
- Check the quality of SVG to PNG conversion with our online SVG to PNG Converter. Upload, convert your files and get results in a few seconds. Try our forceful SVG to PNG Converter for free now!