在 Python 中将 EPS 转换为图像

这篇简短的文章解释了如何在 Python 中将 EPS 转换为 Image。它涵盖了环境设置、算法和在 Python 中将 EPS 转换为 PNG 的代码片段。此外,您无需安装任何图像渲染 UI 或应用程序即可使用此功能。

使用 Python 将 EPS 转换为图像的步骤

  1. 配置 Aspose.Page API 将 EPS 文件导出为图像
  2. 加载输入流后创建 PostScript 类的实例
  3. 启动 ImageSaveOptions 类的对象
  4. 声明 ImageDevice 类的实例并渲染图像
  5. 写入创建的图像文件

这些步骤简化了 Python 中 EPS 到 JPG 的渲染过程。首先,加载 PostScript 文件的输入流并设置不同的属性。接下来,渲染光栅图像并根据您的工作模型将其写入磁盘或流。

使用 Python 将 EPS 转换为 PNG 的代码

from aspose import *
from aspose.page.eps import *
from aspose.page.eps.device import *
import aspose.page
import os
output_file_name = "EPStoImage_out.png"
# Initialize PostScript input stream
ps_stream = open("input.eps", "rb")
document = PsDocument(ps_stream)
# Ignore minor errors
suppress_errors = True
#Initialize ImageSaveOptions object
options = ImageSaveOptions(suppress_errors)
device = ImageDevice(aspose.pydrawing.imaging.ImageFormat.png)
try:
document.save(device, options)
finally:
ps_stream.close()
images_bytes = device.images_bytes
i = 0
for image_bytes in images_bytes:
image_path = os.path.abspath( "EPS_out_image" + str(i) + ".png")
with open(image_path, "wb") as fs:
fs.write(image_bytes[0:0+len(image_bytes)])
i += 1

此代码片段使用 Python 将 EPS 导出为 PNG。您可以对其进行改进,选择在转换过程中抑制小错误,在 ImageDevice 类构造函数中设置图像格式或页面大小。最后,将图像设备中的字节写入磁盘上的文件。

本指南可帮助您了解如何使用 Python 将 EPS 转换为 JPG。此外,如果您需要将 XPS 导出为图像,请阅读 在 Python 中将 XPS 转换为图像 上的文章。

 简体中文