Convert SVG to JPG in Python

To convert SVG to JPG in Python, load the source with SVGDocument, create ImageSaveOptions with ImageFormat.JPEG, and call Converter.convert_svg(). Set a background color when the SVG contains transparent areas because JPG does not support transparency.

JPG and JPEG are two file extensions for the same image format. Converting SVG to JPG creates a raster image that is widely supported by browsers, office applications, content management systems, and image-processing tools. It is suitable for previews and illustrations when a transparent background and editable vector elements are not required.

Convert SVG to JPG in Python

The following example loads the existing tulips.svg file and converts it to JPG with the default rendering settings.

  1. Load the SVG source into an SVGDocument.
  2. Create ImageSaveOptions and select JPEG output.
  3. Call Converter.convert_svg() with the document, options, and output path.
 1import os
 2import aspose.html.dom.svg as ahsvg
 3import aspose.html.converters as conv
 4import aspose.html.rendering.image as rim
 5import aspose.html.saving as sav
 6
 7data_dir = "data"
 8output_dir = "output"
 9os.makedirs(output_dir, exist_ok=True)
10
11input_path = os.path.join(data_dir, "tulips.svg")
12output_path = os.path.join(output_dir, "tulips-default.jpg")
13
14# Select JPEG as the output format
15options = sav.ImageSaveOptions(rim.ImageFormat.JPEG)
16
17# Load the SVG file and convert it to JPG
18with ahsvg.SVGDocument(input_path) as document:
19    conv.Converter.convert_svg(document, options, output_path)

The generated tulips-default.jpg contains pixels rather than SVG paths and shapes. Keep the source SVG if the artwork must remain editable or scale without raster-image limitations.

Convert SVG to JPG Using ImageSaveOptions

The next example creates JPG output with a white background, a 500 × 500 CSS-pixel page, 10 CSS-pixel margins, and a resolution of 150 dpi. These settings provide a predictable canvas and prevent transparent SVG areas from receiving an unintended JPG background.

 1import os
 2import aspose.html.dom.svg as ahsvg
 3import aspose.html.converters as conv
 4import aspose.html.drawing as dr
 5import aspose.html.rendering.image as rim
 6import aspose.html.saving as sav
 7import aspose.pydrawing as pd
 8
 9data_dir = "data"
10output_dir = "output"
11os.makedirs(output_dir, exist_ok=True)
12
13input_path = os.path.join(data_dir, "tulips.svg")
14output_path = os.path.join(output_dir, "tulips-options.jpg")
15
16# Configure JPEG output
17options = sav.ImageSaveOptions(rim.ImageFormat.JPEG)
18options.background_color = pd.Color.white
19options.page_setup.first_page = dr.Page(
20    dr.Size(500, 500),
21    dr.Margin(10, 10, 10, 10)
22)
23options.horizontal_resolution = dr.Resolution.from_dots_per_inch(150.0)
24options.vertical_resolution = dr.Resolution.from_dots_per_inch(150.0)
25
26# Convert SVG to JPG with custom image settings
27with ahsvg.SVGDocument(input_path) as document:
28    conv.Converter.convert_svg(document, options, output_path)

Page dimensions and resolution work together to determine the physical bitmap size. In this example, the 500 × 500 CSS-pixel page rendered at 150 dpi produces a JPG of approximately 781 × 781 pixels.

The following illustration previews the tulip pattern rendered by the example. The Python code writes the actual result in JPG format.

Tulip SVG pattern rendered as a raster image

Useful ImageSaveOptions for JPG

PropertyWhat it controls
formatSelects JPEG output. Use .jpg or .jpeg for the output file.
background_colorFills transparent SVG areas before the result is saved in a format without transparency.
page_setupSets the output page dimensions and margins.
horizontal_resolution and vertical_resolutionSet the output and internal image resolution in pixels per inch.

Common SVG to JPG Issues

ProblemLikely cause and solution
The JPG has a dark or unexpected backgroundThe source contains transparency, which JPG cannot store. Set background_color explicitly.
The image is croppedThe SVG viewport or configured output page is smaller than the visible artwork. Check the SVG viewBox, width, and height, or increase the page size.
Curves or text look blurredThe output dimensions or resolution are too low for the intended display size. Increase the page size or resolution before conversion.
Text uses a substituted fontA font referenced by the SVG is unavailable. Install the font or embed an appropriate font in the SVG.

FAQ

Are JPG and JPEG the same format?

Yes. .jpg and .jpeg are alternative extensions for the same JPEG image format. Use ImageFormat.JPEG with either extension.

Does JPG preserve SVG transparency?

No. JPG does not support transparent pixels. Set ImageSaveOptions.background_color to control how transparent areas are filled.

Is the converted JPG still scalable like SVG?

No. JPG is a raster format with fixed pixel dimensions. Enlarging it beyond the generated size can reduce apparent sharpness.

Related Guides

Other Platforms

Try Online SVG to JPG Conversion

                
            

Use the free online SVG to JPG Converter for quick manual conversion without writing Python code.

Online SVG to JPG converter