Create Raster Images from SVG Graphics
Aspose.HTML for Python via .NET
loads an SVG file or URL with
SVGDocument
and converts it with
Converter.convert_svg()
. Select the raster output through
ImageSaveOptions
.
SVG stores vector shapes and text, while an image export stores a fixed pixel grid. Choose the output dimensions and format for the destination because enlarging the raster file later cannot restore vector scalability.
Convert SVG to an Image and View the Python Code
Choose an SVG file or URL and select the required image format. PNG is used as the visible default example.
{{#if_output 'PDF' 'XPS'}}
import aspose.html.converters as conv
import aspose.html.dom.svg as ahsvg
import aspose.html.saving as sav
{{/if_output}}
{{#if_output 'BMP' 'JPEG' 'GIF' 'PNG' 'TIFF'}}
import aspose.html.converters as conv
import aspose.html.dom.svg as ahsvg
import aspose.html.rendering.image as rim
import aspose.html.saving as sav
{{/if_output}}
with ahsvg.SVGDocument("{{input lower}}") as document:
{{#if_output 'PDF'}}
options = sav.PdfSaveOptions()
{{/if_output}}
{{#if_output 'XPS'}}
options = sav.XpsSaveOptions()
{{/if_output}}
{{#if_output 'BMP' 'JPEG' 'GIF' 'PNG' 'TIFF'}}
options = sav.ImageSaveOptions(rim.ImageFormat.{{output param2 upper}})
{{/if_output}}
conv.Converter.convert_svg(document, options, "output.{{output lower}}")
The example loads input.svg, selects PNG explicitly, and writes output.png. Change ImageFormat and the output extension together when another raster format is required.
Python Code to Convert SVG to PNG
import aspose.html.converters as conv
import aspose.html.dom.svg as ahsvg
import aspose.html.rendering.image as rim
import aspose.html.saving as sav
with ahsvg.SVGDocument("input.svg") as document:
options = sav.ImageSaveOptions(rim.ImageFormat.PNG)
conv.Converter.convert_svg(document, options, "output.png")
How to Convert SVG to an Image in Python
- Install the
aspose-html-netpackage. - Load an SVG file or URL with
SVGDocument. - Create
ImageSaveOptionswith the requiredImageFormat. - Configure page size, resolution, background, CSS, or text rendering when required.
- Call
Converter.convert_svg()and close the SVG document.
Choose the Right SVG Image Format
- PNG: Use for logos, diagrams, text, sharp edges, and transparent backgrounds.
- JPEG: Use for opaque photographic output when lossy compression is acceptable.
- GIF: Use for lightweight, palette-based static images and simple transparency.
- TIFF: Use for publishing, scanning, and image-processing workflows that need TIFF compression choices.
- BMP: Use for simple bitmap compatibility when file size is not the main concern.
Set Image Size, Resolution, Background, and Resources
Configure only the properties required by the destination:
- Page size: Define the raster canvas and margins for the exported SVG content.
- Resolution: Set horizontal and vertical resolution for output and internal raster images.
- Background: Keep transparency for compatible formats or set a color for opaque output.
- CSS and text: Select supported CSS media rules and adjust antialiasing or text rendering when needed.
- Resources: Keep external images, styles, and fonts accessible from the SVG document location.
See the
SVG Converter guide
and
ImageSaveOptions API Reference
.
Install Aspose.HTML for Python via .NET
Install the aspose-html-net package from
PyPI
.
Install Aspose.HTML for Python via .NET
pip install aspose-html-net
Review the installation guide and system requirements before deployment. Request a temporary license to test the API without evaluation limitations.
SVG to Image FAQ
1. Does an image created from SVG remain scalable without quality loss?
No. PNG, JPEG, GIF, BMP, and TIFF store pixels at fixed dimensions. Convert the SVG at the size required by the destination instead of enlarging a small raster result later.
2. Which image format should be selected for SVG conversion?
Use PNG for transparency and sharp graphics, JPEG for opaque photographic output, GIF for simple palette-based images, TIFF for publishing or image processing, and BMP for basic bitmap compatibility.
3. Can Python convert an SVG URL directly to an image?
Yes. Load the accessible SVG URL with SVGDocument, create the required ImageSaveOptions, and pass the document to convert_svg().
SVG and Image Conversion Guides
Choose a specific raster result or continue with a fixed-layout SVG conversion.