Convert SVG to Image in Python

Use SVGDocument to load an SVG file, create ImageSaveOptions, set the image format, and call Converter.convert_svg(document, options, output_path). Use ImageSaveOptions to control the background color, page size, margins, and output resolution.

Aspose.SVG APIs Used

APIPurpose
SVGDocumentLoads the source SVG document
Converter.convert_svg()Converts SVG to raster image formats
ImageSaveOptionsConfigures image output for converter-based workflows
ImageRenderingOptionsConfigures image output for device-based rendering workflows
ImageDeviceReceives rendered SVG content and writes an image file
ImageFormatSelects the target image format

SVG to Image Conversion in Python

Aspose.SVG for Python via .NET can convert SVG documents to raster image formats such as JPG, BMP, TIFF, GIF, and PNG. Use JPG when transparency is not required, PNG for transparent raster output, TIFF for publishing and image-processing workflows, BMP for simple bitmap output, and GIF for lightweight raster output.

For a PNG-focused tutorial, see Convert SVG to PNG in Python. This article focuses on the broader SVG-to-image workflow and format selection.

Before running the examples, install Aspose.SVG for Python via .NET in your Python environment.

Convert SVG to JPG Using Converter.convert_svg()

The following example converts an SVG file to JPG and sets a custom background, page size, margins, and resolution. JPG does not support transparency, so setting a background color is usually important.

 1import os
 2from aspose.svg import SVGDocument
 3from aspose.svg.converters import Converter
 4from aspose.svg.drawing import Margin, Page, Resolution, Size
 5from aspose.svg.rendering.image import ImageFormat
 6from aspose.svg.saving import ImageSaveOptions
 7from aspose.pydrawing import Color
 8
 9# Convert SVG to JPG with custom background, page size, and resolution
10input_folder = "data/"
11output_folder = "output/"
12input_path = os.path.join(input_folder, "christmas-tree.svg")
13output_path = os.path.join(output_folder, "christmas-tree.jpg")
14os.makedirs(output_folder, exist_ok=True)
15
16options = ImageSaveOptions()
17options.format = ImageFormat.JPEG
18options.background_color = Color.from_argb(233, 255, 241)
19options.page_setup.any_page = Page(Size(450, 450), Margin(20, 20, 20, 20))
20options.horizontal_resolution = Resolution.from_dots_per_inch(96.0)
21options.vertical_resolution = Resolution.from_dots_per_inch(96.0)
22
23with SVGDocument(input_path) as document:
24    Converter.convert_svg(document, options, output_path)

The following image shows the source SVG and the converted JPG result with a custom background color.

Original SVG image and converted JPG image with a custom background

Change the Output Image Format

To save SVG as another image format, set options.format and use the matching file extension. For example, use ImageFormat.GIF with .gif, ImageFormat.BMP with .bmp, or ImageFormat.TIFF with .tiff.

1options.format = ImageFormat.GIF
2output_path = os.path.join(output_folder, "image.gif")

Common ImageSaveOptions Properties

PropertyWhat it controls
formatTarget image format such as JPG, PNG, BMP, TIFF, or GIF
background_colorBackground color for transparent SVG areas
page_setupOutput image size, margins, and page layout
horizontal_resolutionHorizontal output resolution in pixels per inch
vertical_resolutionVertical output resolution in pixels per inch
smoothing_modeRendering quality for shapes and edges
compressionTIFF compression settings

ImageSaveOptions inherits image rendering settings from ImageRenderingOptions, so many output properties are shared between converter-based and device-based workflows.

Convert SVG to Image Using render_to()

The render_to() method sends SVG content to an image rendering device. This pattern is useful when your application already uses rendering devices or needs to select the target device explicitly.

 1import os
 2from aspose.svg import SVGDocument
 3from aspose.svg.drawing import Margin, Page, Resolution, Size
 4from aspose.svg.rendering.image import ImageDevice, ImageFormat, ImageRenderingOptions
 5
 6# Render SVG to JPG through an ImageDevice
 7input_folder = "data/"
 8output_folder = "output/"
 9input_path = os.path.join(input_folder, "image.svg")
10output_path = os.path.join(output_folder, "image.jpg")
11os.makedirs(output_folder, exist_ok=True)
12
13options = ImageRenderingOptions()
14options.format = ImageFormat.JPEG
15options.horizontal_resolution = Resolution.from_dots_per_inch(96.0)
16options.vertical_resolution = Resolution.from_dots_per_inch(96.0)
17options.page_setup.any_page = Page(Size(600, 600), Margin(10, 10, 10, 10))
18
19with SVGDocument(input_path) as document:
20    with ImageDevice(options, output_path) as device:
21        document.render_to(device)

Common Mistakes and Fixes

ProblemLikely causeFix
JPG output has a black or unexpected backgroundJPG does not support transparencySet options.background_color before conversion
The output image is cropped or too largePage setup does not match the SVG viewport or desired output sizeConfigure page_setup.any_page with Page, Size, and Margin
The image is blurryOutput resolution or page size is too lowIncrease horizontal_resolution, vertical_resolution, or output dimensions
The wrong format is producedThe image format and file extension do not matchSet both options.format and the output file extension deliberately
External images or CSS are missingRelative resources cannot be resolved during conversionKeep resources next to the SVG file or use correct absolute paths

Online SVG to Image Converter

Use the free online SVG to JPG Converter or the general SVG Converter to test SVG-to-image conversion before implementing it in Python.

                
            

Online SVG to JPG converter

FAQ

How do I convert SVG to JPG in Python?

Load the SVG with SVGDocument, set ImageSaveOptions.format to ImageFormat.JPEG, and call Converter.convert_svg(document, options, output_path).

Can I convert SVG to BMP, TIFF, or GIF with the same code?

Yes. Change options.format to the required ImageFormat value and use the matching output file extension.

Why should I set a background color for JPG output?

JPG does not support transparency. A background color makes transparent SVG areas predictable in the converted image.

Can I control the output image size?

Yes. Configure options.page_setup.any_page with a Page, Size, and Margin, and set the resolution if needed.

Should I use Converter.convert_svg() or render_to()?

Use Converter.convert_svg() for ordinary file conversion. Use render_to() when you need a rendering-device workflow with ImageDevice.

Related Articles