Convert SVG to Image, JPG, PNG, BMP, TIFF and GIF using Python
In the article, you will find information on how to convert SVG to Image file formats such as JPG, PNG, BMP, TIFF, and GIF. Image file conversion is required for website developing, graphic designers acting, photography, and other purposes. The choice of the image format depends on whether you print it in polygraphy, send it by e-mail or put an image on a webpage.
The article provides a general description of the conversion features of Aspose.SVG for Python via .NET and describes supported scenarios of SVG to Image conversions by using Converter and SVGDocument classes.
To continue following this tutorial, you should install and configure the Aspose.SVG for Python via .NET library in your Python project. Our code examples help you to convert SVG files using the Python library.
Online SVG Converter
You can convert SVG to images and other popular formats in any way – online or programmatically. Check the Aspose.SVG API functionality and convert SVG in real-time! Please load SVG from a local file system or URL, select the output format and run the example. In the example, the save options are set by default. You will immediately receive the result as a separate file.
If you want to convert SVG to image formats programmatically in Python, please see the following conversion scenarios and Python code examples.
Convert SVG to JPG
The JPG format offers high compression rates with minimal loss of quality, making it ideal for use on the web where file size matters. It is widely supported across devices and platforms, ensuring compatibility and ease of sharing. Additionally, JPG’s ability to handle complex color variations makes it ideal for photographs and realistic images.
Using the convert_svg()
method
Using convert_svg() methods is the most common way to convert SVG to various popular formats. You can convert SVG to JPG or to another format programmatically with full control over a wide range of conversion parameters. The following code snippet shows how to convert SVG to JPG and specify background color, page size, margins, and vertical & horizontal resolutions. In addition, the following Python example will show how to configure the paths to the source and output files in your file system:
- Use the
set_extension() method of the
Configuration
class to register theSkiaSharp
extension. TheSkiaSharp
module is a graphics library used for rendering SVG content. It ensures that the rendering engine supports the operations needed for the conversion. - Create an instance of the
ImageSaveOptions class and specify required save options:
- Use the
format
property to set image format. - Use the
background_color
property to set the color that will fill the background. - Use the
page_setup
property to set paage size amd margins. - Use the
horizontal_resolution
andvertical_resolution
properties to set the horizontal and vertical resolutions for output image.
- Use the
- Open a source SVG document using the SVGDocument class.
- Use the convert_svg() method to convert and save SVG as an image file.
1import os
2import aspose
3from aspose.svg import *
4from aspose.svg.converters import *
5from aspose.svg.drawing.skiasharp import *
6from aspose.svg.rendering import *
7from aspose.svg.drawing import *
8from aspose.svg.saving import *
9
10# Initialize an SVG document from a file
11input_folder = "data/"
12output_folder = "output/"
13src_file = os.path.join(input_folder, "сhristmas-tree.svg")
14output_file = os.path.join(output_folder, "сhristmas-tree.jpg")
15if not os.path.exists(output_folder):
16 os.makedirs(output_folder)
17
18# Activate the Aspose.SVG.Drawing.SkiaSharp feature
19Configuration.set_extension(SkiaModule())
20options = ImageSaveOptions()
21options.format.JPEG
22options.background_color = aspose.pydrawing.Color.from_argb(233, 255, 241)
23options.page_setup.any_page = Page(Size(450, 450), Margin(20, 20, 20, 20))
24options.horizontal_resolution = Resolution.from_dots_per_inch(96.0)
25options.vertical_resolution = Resolution.from_dots_per_inch(96.0)
26with SVGDocument(src_file) as document:
27
28 # Convert SVG to JPG
29 Converter.convert_svg(document, options, output_file)
You can evaluate the quality of conversion by trying our product. Here, we provide an illustration - the following Figure shows the original сhristmas-tree.svg image (a) and the converted the сhristmas-tree.jpg image with a new background color (b):
How to convert SVG to image
Aspose.SVG for Python via .NET supports converting SVG to image formats such as PNG, JPG, JPEG, BMP, TIFF, GIF, and WEBP. You can use the above Python code for this; to change the output image format, you only need to specify the required extension (format) in the output file name and set the format
property.
For example, to convert SVG to GIF
, you need:
- to set the
format
property:options.format.GIF
, - and set the extension
.gif
in the output image file name:output_file = os.path.join(output_folder, "image.gif")
.
Image Save Options – ImageSaveOptions class
Aspose.SVG allows converting SVG to Image file formats using default or custom save options. ImageSaveOptions usage enables you to customize the rendering process. For example, you can specify the image format, page size, margins, background color, etc.
Property | Description |
---|---|
compression | Sets Tagged Image File Format (TIFF) Compression. By default, this property is LZW. |
css | Gets a CssOptions object which is used for configuration of CSS properties processing. |
format | Sets the ImageFormat (JPG, PNG, BMP, TIFF, or GIF). By default, this property is PNG. |
background_color | This property sets the color that will fill the background. By default, this property is transparent. |
page_setup | This property allows you to define the layout of the page, including dimensions and margins. |
horizontal_resolution | Sets the horizontal resolution for output and internal images in pixels per inch (dpi). By default, this property is set to 300 dpi, which is used unless overridden by specific conditions. The resolution is always applied unless the Page size is set in pixels (px), in which case the default resolution is 96 dpi. |
vertical_resolution | Sets the vertical resolution for output and internal images in pixels per inch (dpi). By default, this property is set to 300 dpi, which is used unless overridden by specific conditions. The resolution is always applied unless the Page size is set in pixels (px), in which case the default resolution is 96 dpi. |
smoothing_mode | This property sets the rendering quality for this image. |
text | Gets a TextOptions object which is used for configuration of text rendering. |
Note: The options that are implementing with the ImageSaveOptions class are inheriting from the ImageRenderingOptions class.
Using the render_to()
method
Consider how to convert a document from SVG to JPG using render_to()
method:
- Initialize an SVG document using the SVGDocument() class.
- Create an instance of the
ImageRenderingOptions class. If you need to set image format, horizontal and vertical resolutions, page size and margins:
- Use the
format
property to set image format. - Use the
horizontal_resolution
andvertical_resolution
properties to set the horizontal and vertical resolutions for output image. - Use the
page_setup
property to set page size amd margins.
- Use the
- Create a new instance of the ImageDevice class.
- Convert SVG to JPG using the
render_to(
device
) method of the SVGDocument class.
The following example shows how to apply the page_setup
, horizontal_resolution
and vertical_resolution
properties for SVG to JPG converting:
1import os
2from aspose.svg import *
3from aspose.svg.drawing.skiasharp import *
4from aspose.svg.rendering import *
5from aspose.svg.drawing import *
6from aspose.svg.rendering.image import *
7
8# Initialize an SVG document from a file
9input_folder = "data/"
10output_folder = "output/"
11src_file = os.path.join(input_folder, "image.svg")
12output_file = os.path.join(output_folder, "image.jpg")
13if not os.path.exists(output_folder):
14 os.makedirs(output_folder)
15
16with SVGDocument(src_file) as document:
17 # Initialize an instance of the ImageRenderingOptions class and set custom properties
18 image_rendering_options = ImageRenderingOptions()
19 image_rendering_options.format = ImageFormat.JPEG
20 image_rendering_options.horizontal_resolution = Resolution.from_dots_per_inch(96.0)
21 image_rendering_options.vertical_resolution = Resolution.from_dots_per_inch(96.0)
22 image_rendering_options.page_setup.any_page = Page(Size(600, 600), Margin(10, 10, 10, 10))
23
24 # Initialize an instance of the ImageDevice class
25 with ImageDevice(image_rendering_options, output_file) as device:
26 # Render SVG to JPG and send the document to the rendering device
27 document.render_to(device)
You can use the above Python code for converting SVG to image formats such as PNG, JPG, JPEG, BMP, TIFF, GIF, and WEBP. To change the output image format, you need to specify the required extension (format) in the output file name and set the format
property.
For example, to convert SVG to BMP
, you only need to set the format
property:
image_rendering_options.format = ImageFormat.BMP
,
and set the extension .bmp
in the output image file name:
output_file = os.path.join(output_folder, "image.bmp")
.
Image Rendering Options – ImageRenderingOptions class
The
ImageRenderingOptions class in Aspose.SVG for Python via .NET provides a set of options to control how SVG documents are rendered to image formats. The ImageRenderingOptions
class is used in conjunction with the specific device ImageDevice
, which represents the target output image formats for the rendered SVG content.
This class allows you to fine-tune the rendering process to meet specific requirements. Here are key properties associated with the ImageRenderingOptions class:
Property | Description |
---|---|
compression | Sets Tagged Image File Format (TIFF) Compression. By default, this property is LZW. |
css | This property gets a CssOptions object, which is used for the configuration of CSS properties processing. |
format | Sets the ImageFormat (JPG, PNG, BMP, TIFF, or GIF). By default, this property is PNG. |
background_color | This property allows you to set the background color for the rendered output. If not set, the default background is transparent. |
page_setup | This property allows you to define the layout of the page, including dimensions and margins. |
horizontal_resolution | Sets the horizontal resolution for output and internal images in pixels per inch (dpi). By default, this property is set to 300 dpi, which is used unless overridden by specific conditions. The resolution is always applied unless the Page size is set in pixels (px), in which case the default resolution is 96 dpi. |
vertical_resolution | Sets the vertical resolution for output and internal images in pixels per inch (dpi). By default, this property is set to 300 dpi, which is used unless overridden by specific conditions. The resolution is always applied unless the Page size is set in pixels (px), in which case the default resolution is 96 dpi. |
smoothing_mode | This property sets the rendering quality for this image. |
text | Gets a TextOptions object which is used for configuration of text rendering. |
You can try our free online SVG to JPG Converter that works with high quality, easy and fast. Just upload SVG, convert it, and get results in seconds! It’s fast, easy, and completely free!