医术绝伦的顾北寒 發表於 2025-10-15 10:55:00

CAD文件处理控件Aspose.CAD教程:在Python中将SVG转换为PNG

<p><img src="https://image.evget.com/attachment/keditor/image/20251015/095318_9.png"></p>
<p>缩放SVG文件而不会损失图像质量,因此,SVG文件格式被广泛用于徽标和图标。在某些情况下,您确实需要将 SVG 转换为其他图像文件格式,以便与打印应用程序兼容。</p>
<p>本篇教程,我们将学习如何使用<strong>Aspose.CAD for Python via .NET</strong>将 SVG 转换为PNG 。这款 3D CAD SDK 有助于实现单个文件和多个文件格式转换的自动化。我们将编写一个完整的代码示例,以演示<strong>Aspose.CAD for Python via .NET</strong>的可用性和效率。</p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong>Aspose.CAD 试用版下载,请联系Aspose官方授权代理商慧都科技</strong></span></p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong><em>加入Aspose技术交流QQ群(</em></strong><em>1041253375</em><strong><em>),与更多小伙伴一起探讨提升开发技能。</em></strong></span></p>
<h2 id="3d-cad-sdk-installation">3D CAD SDK 安装</h2>
<p><strong>Aspose.CAD for Python via .NET</strong>专门用于处理 CAD 图纸。它的安装过程非常简单。只需<strong>联系慧都科技下载SDK 文件</strong>或在 CMD 中运行以下命令:</p>
<pre class="prettyprint highlighter-hljs"><code>pip install aspose-cad</code></pre>
<h2 id="code">使用 Python 将 SVG 转换为 PNG - 代码示例</h2>
<p>除了转换功能外,这款强大的 SDK 还允许您选择特定图层并跟踪转换过程。现在,让我们开始查看代码步骤和代码示例。</p>
<p>步骤:</p>
<ul>
<li>通过调用load方法从给定的输入路径加载 SVG 文件。</li>
<li>配置光栅化选项以将矢量转换为光栅。</li>
<li>设置输出图像的宽度、高度和缩放级别。</li>
<li><strong>创建PngOptions</strong>类的对象。</li>
<li>使用配置的光栅化将加载的 CAD 图像保存为 PNG。</li>
</ul>
<p>使用下面给出的代码片段在 Python 中将 SVG 更改为 PNG:</p>
<pre class="prettyprint lang-py highlighter-hljs"><code>import aspose.cad as cad
from aspose.cad.imageoptions import PngOptions

# A class to handle CAD image conversion using Aspose.CAD for Python via .NET
class CadImageConverter:
    def __init__(self, input_path, output_path, license_path):
      # Store input, output, and license file paths
      self.input_path = input_path
      self.output_path = output_path
      self.license_path = license_path
      self.image = None               
      self.raster_options = None      

    def apply_license(self):
      # Apply the Aspose.CAD license to unlock full functionality
      print("Applying Aspose.CAD license...")
      license_obj = cad.License()
      license_obj.set_license(self.license_path)
      print("License applied successfully.")

    def load_cad_image(self):
      # Load a CAD (or SVG) file from the given input path
      print(f"Loading CAD image froms: {self.input_path}")
      self.image = cad.Image.load(self.input_path)
      print("Image loaded successfully.")

    def configure_rasterization(self, width=800.5, height=800.5, zoom=0.5, layers="Layer"):
      # Configure rasterization options for converting vector to raster
      print("Configuring rasterization options...")
      self.raster_options = cad.imageoptions.CadRasterizationOptions()
         # Set output image width
      self.raster_options.page_width = width
         # Set output image height   
      self.raster_options.page_height = height
      # Define zoom level   
      self.raster_options.zoom = zoom
      # Specify which layers to render            
      self.raster_options.layers = layers         
      print("Rasterization configured.")

    def save_as_png(self):
      # Save the loaded CAD image as a PNG using the configured rasterization
      print(f"Saving image as PNG to: {self.output_path}")
      png_options = PngOptions()
      png_options.vector_rasterization_options = self.raster_options
      self.image.save(self.output_path, png_options)
      print("Image saved as PNG successfully.")


def main():
    input_svg = "sample1.svg"      
    output_png = "result-file.png"
    license_file = "license.lic"   

    # Initialize converter with file paths
    converter = CadImageConverter(input_svg, output_png, license_file)

    # Apply license, load the image, configure rasterization, and save as PNG
    converter.apply_license()
    converter.load_cad_image()
    converter.configure_rasterization()
    converter.save_as_png()


# Run the script if executed directly.
if __name__ == "__main__":
    main()</code></pre>
<p>输出:<img src="https://image.evget.com/attachment/keditor/image/20251015/100048_8.png"></p>
<p>总之,只需几行代码,您就可以为您的应用程序开发一个 SVG 到 PNG 的转换器。我们已经完成了如何在 Python 中以编程方式将 SVG 转换为 PNG 的实现过程。通过 .NET 为 Python 编写的<strong>&nbsp;Aspose.CAD</strong>非常直观,您可以根据需要轻松配置流程。</p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong>Aspose.CAD 试用版下载,请联系Aspose官方授权代理商慧都科技</strong></span></p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong><em>加入Aspose技术交流QQ群(</em></strong><em>1041253375</em><strong><em>),与更多小伙伴一起探讨提升开发技能。</em></strong></span></p><br><br>
来源:https://www.cnblogs.com/software-Development/p/19142822
頁: [1]
查看完整版本: CAD文件处理控件Aspose.CAD教程:在Python中将SVG转换为PNG