3D CAD SDK 安装
Aspose.CAD for Python via .NET专门用于处理 CAD 图纸。它的安装过程非常简单。只需联系慧都科技下载SDK 文件或在 CMD 中运行以下命令:
pip install aspose-cad使用 Python 将 SVG 转换为 PNG - 代码示例
除了转换功能外,这款强大的 SDK 还允许您选择特定图层并跟踪转换过程。现在,让我们开始查看代码步骤和代码示例。
步骤:
- 通过调用load方法从给定的输入路径加载 SVG 文件。
- 配置光栅化选项以将矢量转换为光栅。
- 设置输出图像的宽度、高度和缩放级别。
- 创建PngOptions类的对象。
- 使用配置的光栅化将加载的 CAD 图像保存为 PNG。
使用下面给出的代码片段在 Python 中将 SVG 更改为 PNG:
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()输出:
