本教程介绍如何在 Python 中将 SVG 转换为 PNG。它涵盖了在 Python 中将 SVG 转换为 PNG 的算法和示例代码。您可以通过进行一些 API 调用来执行转换,而无需安装任何图像处理软件。
使用 Python 将 SVG 转换为 PNG 的步骤
- 安装 Aspose.SVG 将 SVG 渲染为 PNG 图像
- 激活 SkiaSharp 命名空间的功能
- 实例化 ImageSaveOptions 类的对象并设置 ImageFormat
- 使用 SVGDocument 类加载输入 SVG 图像
- 将 SVG 转换为 PNG 图像
这些步骤总结了在 Python 中将 SVG 转换为 PNG 的算法。第一步,使用 SkiaSharp 扩展设置渲染引擎。随后,根据您的要求指定输出图像格式并将输出渲染到指定路径。
使用 Python 将 SVG 保存为 PNG 的代码
import aspose.svg | |
from aspose.svg import * | |
from aspose.svg.converters import * | |
from aspose.svg.drawing.skiasharp import * | |
from aspose.svg.saving import * | |
# Activate the Aspose.SVG.Drawing.SkiaSharp feature | |
Configuration.set_extension(SkiaModule()) | |
options = ImageSaveOptions() | |
options.format = aspose.svg.rendering.image.ImageFormat.PNG | |
with SVGDocument("svg.svg") as document: | |
# Convert SVG to PNG | |
Converter.convert_svg(document, options, "SVGtoPNG.png") |
上面的代码片段足以在 Python 中批量将 SVG 转换为 PNG。您只需添加适当的循环并根据需要对尽可能多的矢量图像进行迭代即可。此外,您可以调整输出图像格式,例如 JPEG、BMP、GIF、TIFF 等,以更好地满足您的要求。
在本文中,您了解了在 Python 中将 SVG 保存为 PNG 的详细信息。如果您对编辑 SVG 图像(如更改背景)感兴趣,请访问文章 在 Python 中更改 SVG 的背景颜色。