用 Python 将 SVG 转换为 PNG

PNG 文件格式支持无损图像压缩,因此深受用户欢迎。SVG 是一种矢量格式,所有平台和应用程序可能都不支持,而 PNG 则不同,它具有通用性,可在各种软件、网站和设备上轻松使用。此外,PNG 支持透明背景,是网页图形、徽标和图像的理想选择。使用 Aspose.HTML for Python via .NET,您可以通过编程将 SVG 转换为 PNG 格式,并完全控制各种转换参数。

在这篇文章中,你将找到通过使用 Converter 类的 convert_svg() 方法和应用 ImageSaveOptions 将 SVG 转换为 PNG 的信息。此外,您还可以尝试使用在线 SVG 转换器来测试 Python API 的功能并即时转换 SVG。

在线 SVG 转换器

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

                
            

将 SVG 转换为 PNG

使用 convert_svg() 方法是将 SVG 转换为各种格式的最常用方法。要进行转换,可以从文件、URL 或代码字符串加载 SVG。在下面的示例中,我们从代码中创建一个 SVG 文件。

  1. 为 SVG 文档编写代码
  2. 创建一个新的 ImageSaveOptions 对象。默认情况下,“format “属性为 PNG。如果您没有为保存生成的图像设置特定选项,则将使用默认选项。
  3. 使用转换器类的 convert_svg(content, base_uri, options, output_path) 方法。
 1# Convert SVG to PNG using Python
 2
 3import os
 4import aspose.html.converters as conv
 5import aspose.html.saving as sav
 6
 7# Setup directories and define paths
 8output_dir = "output/"
 9if not os.path.exists(output_dir):
10    os.makedirs(output_dir)
11save_path = os.path.join(output_dir, "circle.png")
12
13# Prepare SVG code
14svg_code = """<svg xmlns="http://www.w3.org/2000/svg">
15<circle cx="100" cy="100" r="60" fill="teal" stroke="salmon" stroke-width="10" />
16</svg>"""
17
18# Initialize ImageSaveOptions
19options = sav.ImageSaveOptions()
20
21# Convert SVG to PNG
22conv.Converter.convert_svg(svg_code, ".", options, save_path)

使用 ImageSaveOptions 将 SVG 转换为 PNG

PNG 图像创建功能可根据需要通过保存选项进行增强。ImageSaveOptions](6) 类为将 SVG 内容转换为图像格式提供了广泛的自定义功能。以下是对 ImageSaveOptions 各属性的说明:

要将 SVG 转换为 PNG 并指定 “ImageSaveOptions”(图像保存选项),应遵循以下几个步骤:

  1. 使用 SVGDocument 类的 SVGDocument() 构造函数之一加载 SVG 文件。( tulips.svg).
  2. 创建一个新的 ImageSaveOptions 对象并指定保存选项。默认情况下,format 属性为 PNG。ImageSaveOptions() 构造函数会初始化一个 ImageSaveOptions 类实例,并将其传递给 convert_svg() 方法。在这里,您可以设置所需的保存选项,如格式或分辨率。
  3. 使用 convert_svg() 方法之一将 SVG 保存为 PNG 图像。在示例中,convert_svg() 方法接收 documentoptions 和输出文件路径 save_path 并执行转换操作。
 1# Convert SVG to PNG with custom settings using Python
 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.drawing as dr
 8
 9# Setup directories and define paths
10output_dir = "output/"
11input_dir = "data/"
12if not os.path.exists(output_dir):
13    os.makedirs(output_dir)
14
15document_path = os.path.join(input_dir, "tulips.svg")
16save_path = os.path.join(output_dir, "tulips.png")
17
18# Load an SVG document
19document = ahsvg.SVGDocument(document_path)
20
21# Initialize ImageSaveOptions
22options = sav.ImageSaveOptions()
23options.page_setup.first_page = dr.Page(dr.Size(500, 500), dr.Margin(10, 10, 10, 10))
24options.css.media_type.PRINT
25
26# Convert SVG to PNG
27conv.Converter.convert_svg(document, options, save_path)

在上述示例中,我们使用

图中以 tulips.png 文件的一个片段为例,展示了 SVG 到 PNG 的渲染质量。

文本 “tulips.png 图片”

如何将 SVG 转换为图像

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

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

另请参见

文本 “SVG 到 PNG 转换器”

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.