用 Python 将 SVG 转换为 JPG

JPG/JPEG 是一种受不同平台、设备和应用程序广泛支持的图像格式,具有广泛的兼容性。将 SVG 转换为 JPG 可以更方便地将图像集成到可能不支持 SVG 文件的文档和演示文稿中。使用 Aspose.HTML for Python via .NET,您可以通过编程将 SVG 转换为 JPG 格式,并完全控制各种转换参数。

本文将介绍如何使用 Converter 类的 convert_svg() 方法将 SVG 转换为 JPG,以及如何应用 ImageSaveOptions。此外,您还可以试用在线 SVG 转换器来测试 Python API 的功能并即时转换 SVG。

在线 SVG 转换器

您可以使用 Aspose.HTML 将 SVG 实时转换为其他格式。从本地系统或 URL 加载 SVG 文件,选择所需的输出格式,然后运行示例。保存选项为默认设置。您将立即以单独文件的形式收到转换结果。

                
            

如果您想通过编程将 SVG 转换为 JPG 图像,请参阅以下 Python 代码示例。

将 SVG 转换为 JPG

在下面的 Python 示例中,我们通过代码创建一个 SVG 文件,并将其转换为 JPG 图像。

  1. 为 SVG 文档编写代码
  2. 创建一个具有 JPEG 图像格式的新 ImageSaveOptions 对象。默认情况下,format 属性为 PNG。如果您没有为保存生成的图像设置特定选项,则将使用默认选项。
  3. 使用转换器类的 convert_svg(content, base_uri, options, output_path) 方法将 SVG 保存为 JPG 图像。
 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)

使用 ImageSaveOptions 将 SVG 转换为 JPG

ImageSaveOptions 类提供了大量属性,可让你完全控制各种参数,并改善将 SVG 转换为图像格式的过程。要使用 ImageSaveOptions 指定将 SVG 转换为 JPG,需要遵循以下几个步骤:

  1. 使用 SVGDocument 类的 SVGDocument() 构造函数之一加载 SVG 文件。( tulips.svg).
  2. 创建一个具有 JPEGformat 属性的 ImageSaveOptions 类实例。在这里,您可以设置所需的保存选项,如页面设置或分辨率。
  3. 使用 convert_svg() 方法之一将 SVG 保存为 JPG 图像。在下面的示例中,convert_svg() 方法接收 document, options, 输出文件路径 save_path 并执行转换操作。

下面的 Python 代码片段展示了如何使用自定义保存选项将 SVG 转换为 JPG:

 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)

在上述示例中,我们使用

下图显示了 “svg-to-image.jpeg “文件的片段。

文本 “svg-to-image.jpeg 图像”

如何将 SVG 转换为图像

Aspose.HTML for Python via .NET 支持将 SVG 转换为 PNG、JPEG、BMP、TIFF 和 GIF 图像。要设置输出图像格式,只需在输出文件名中指定所需的扩展名(格式),并设置保存选项对象的 format 属性即可。

例如,要将 SVG 转换为 BMP,您需要

另请参见

文本 “SVG 到 JPG 转换器”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.