Convert SVG to JPG in Python

Quick Answer: To convert SVG to JPG in Python, load the source file with SVGDocument, create ImageSaveOptions, set options.format = ImageFormat.JPEG, and call Converter.convert_svg(document, options, output_path). Set a background color because JPG does not support transparency.

Convert SVG to JPG Programmatically

JPG is useful when you need a compact raster preview, thumbnail, report image, or web asset and transparency is not required. If the SVG contains transparent areas, choose an explicit background color before conversion. Otherwise the final background may depend on the renderer or viewing workflow.

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

Convert SVG to JPG Using Default Settings

The following example loads document.svg, selects JPEG output, and saves the result as document.jpg.

 1from aspose.svg import SVGDocument
 2from aspose.svg.converters import Converter
 3from aspose.svg.rendering.image import ImageFormat
 4from aspose.svg.saving import ImageSaveOptions
 5
 6# Convert an SVG file to JPG
 7options = ImageSaveOptions()
 8options.format = ImageFormat.JPEG
 9
10with SVGDocument("document.svg") as document:
11    Converter.convert_svg(document, options, "document.jpg")

The example uses SVGDocument to load the source file, ImageSaveOptions with ImageFormat.JPEG to select JPG output, and Converter.convert_svg() to write the converted file.

Convert SVG to JPG with Custom Options

Use ImageSaveOptions when the JPG needs a specific canvas, background, or resolution. The next example sets a light background, creates a 450 x 450 page with margins, and renders at 96 dpi.

 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, "flower.svg")
13output_path = os.path.join(output_folder, "flower.jpg")
14os.makedirs(output_folder, exist_ok=True)
15
16options = ImageSaveOptions()
17options.format = ImageFormat.JPEG
18options.background_color = Color.from_argb(240, 240, 240)
19options.page_setup.any_page = Page(Size(350, 350), Margin(10, 10, 10, 10))
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 an SVG source (a) and the converted JPG result (b) with a custom background color.

Original SVG image and converted JPG image with a custom background

Important ImageSaveOptions Properties

PropertyWhen to use it
formatSelect JPEG output with ImageFormat.JPEG
background_colorFill transparent SVG areas before saving to JPG
page_setupSet page dimensions, margins, and canvas layout
horizontal_resolutionSet horizontal output resolution in dpi
vertical_resolutionSet vertical output resolution in dpi
smoothing_modeControl rasterization quality for shapes and edges

Common Mistakes and Fixes

ProblemLikely causeFix
JPG output has a black or unexpected backgroundThe source SVG has transparent areas, but JPG cannot store transparencySet options.background_color before conversion
The image is blank or croppedThe SVG has no usable viewport, or the output page is smaller than the visible contentDefine SVG width, height, or viewBox, or configure options.page_setup.any_page
Linked images, CSS, or fonts are missingRelative resources cannot be resolved from the current input pathLoad the SVG from its original file path and keep linked resources in the expected relative locations
The file extension and actual format do not matchoptions.format was not set to ImageFormat.JPEG, or the output path uses the wrong extensionSet options.format = ImageFormat.JPEG and save to .jpg or .jpeg
Lines, icons, or text look jaggedThe output dimensions or raster resolution are too low for the target display sizeIncrease page dimensions or resolution, and check smoothing_mode for quality-sensitive output

Online SVG to JPG Converter

Use the free online SVG to JPG Converter to test output before implementing the same workflow 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).

Should I use JPG or PNG for SVG conversion?

Use JPG for compact previews, thumbnails, and web images when transparency is not required. Use PNG when you need lossless raster output or transparent areas.

Is the converted JPG still an editable vector image?

No. JPG is a raster image format. After conversion, the output contains pixels, not editable SVG elements, paths, or text nodes.

Can I save the output as .jpeg instead of .jpg?

Yes. Use ImageFormat.JPEG and choose either .jpg or .jpeg in the output file name, depending on the naming convention your workflow expects.

Can I convert SVG from a URL or memory string to JPG?

Yes. Create an SVGDocument from a URL, file stream, or SVG markup string with a valid base URI, then pass the document to Converter.convert_svg() with JPEG image options.

Can I convert many SVG files to JPG?

Yes. Loop through the input folder, use the same ImageSaveOptions, and call Converter.convert_svg() for each SVG file. See Batch Convert SVG Files.

Related Articles