Convert SVG to JPG in Python

JPG/JPEG is a widely supported image format across different platforms, devices, and applications, offering broad compatibility. Converting SVG to JPG makes it easier to integrate images into documents and presentations that may not support SVG files. With Aspose.HTML for Python via .NET, you can convert SVG to JPG format programmatically with full control over a wide range of conversion parameters.

In this article, you will find information on how to convert SVG to JPG using convert_svg() methods of the Converter class and how to apply 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 with Aspose.HTML in real time. Load an SVG file from your local system or URL, select the desired output format, and run the example. The save options are set by default. You will immediately receive the conversion result as a separate file.

                
            

If you want to convert SVG to JPG image programmatically, please see the following Python code examples.

Convert SVG to JPG

In the following Python example, we create an SVG file from code and convert it to JPG image.

  1. Prepare code for an SVG document.
  2. Create a new ImageSaveOptions object with JPEG ImageFormat. 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.
  3. Use the convert_svg(content, base_uri, options, output_path) method of the Converter class to save SVG as a JPG image.
 1# Convert SVG code to JPG image using Python
 2
 3import os
 4import aspose.html.converters as conv
 5import aspose.html.saving as sav
 6import aspose.html.rendering.image as rim
 7
 8# Setup directories and define paths
 9output_dir = "output/"
10if not os.path.exists(output_dir):
11    os.makedirs(output_dir)
12save_path = os.path.join(output_dir, "circle.jpg")
13
14# Prepare SVG code
15svg_code = """<svg xmlns="http://www.w3.org/2000/svg">
16<circle cx="100" cy="100" r="70" fill="teal" stroke="pink" stroke-width="10" />
17</svg>"""
18
19# Initialize ImageSaveOptions
20options = sav.ImageSaveOptions(rim.ImageFormat.JPEG)
21
22# Convert SVG to JPG
23conv.Converter.convert_svg(svg_code, ".", options, save_path)

Convert SVG to JPG using ImageSaveOptions

The ImageSaveOptions class provides numerous properties that give you full control over a wide range of parameters and improve the process of converting SVG to Image formats. To convert SVG to JPG with ImageSaveOptions specifying, you should follow a few steps:

  1. Load an SVG file using one of the SVGDocument() constructors of the SVGDocument class. ( tulips.svg).
  2. Create an instance of the ImageSaveOptions class with JPEG format propery. Here, you can set the required save options, such as page setup or resolution.
  3. Use one of the convert_svg() methods to save SVG as a JPG image. In the following example, the convert_svg() method takes the document, options, output file path save_path and performs the conversion operation.

The following Python code snippet shows how to convert SVG to JPG using custom save options:

 1# Convert SVG to JPG using Python with custom settings
 2
 3import os
 4import aspose.html.dom.svg as ahsvg
 5import aspose.html.converters as conv
 6import aspose.html.saving as sav
 7import aspose.html.rendering.image as rim
 8import aspose.html.drawing as dr
 9
10
11# Setup directories and define paths
12output_dir = "output/"
13input_dir = "data/"
14if not os.path.exists(output_dir):
15    os.makedirs(output_dir)
16
17document_path = os.path.join(input_dir, "tulips.svg")
18save_path = os.path.join(output_dir, "svg-to-image.jpg")
19
20# Load an SVG document
21document = ahsvg.SVGDocument(document_path)
22
23# Initialize ImageSaveOptions
24options = sav.ImageSaveOptions(rim.ImageFormat.JPEG)
25options.horizontal_resolution = dr.Resolution.from_dots_per_inch(200.0)
26options.vertical_resolution = dr.Resolution.from_dots_per_inch(200.0)
27
28# Convert SVG to JPG
29conv.Converter.convert_svg(document, options, save_path)

In the above example, we use:

The figure illustrates the fragment of the svg-to-image.jpeg file.

Text “svg-to-image.jpeg image”

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 BMP, you need:

See Also

Text “SVG to JPG Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.