在 PS 文件中处理图像 | Python
Contents
[
Hide
Show
]在 PS 文档中添加图像
Aspose.Page for Python via .NET 库提供了两种将图像合并到 PS 文档中的方法:
- 用于不透明图像;
- 用于透明图像;
之所以出现这种区别,是因为 PostScript 本身并不支持透明度。然而,半透明图像可以表示为完全透明和完全不透明像素的组合,称为蒙版。将半透明图像添加到 PS 文档时,必须进行检查和预处理,以确保准确反映透明度。此过程需要额外的时间。因此,如果已知图像完全不透明,则使用第一种方法更高效,可以节省执行时间。
第二种方法会自动判断图像是完全不透明、完全透明还是半透明。如果图像完全不透明,则使用第一种方法添加。如果图像完全透明,则将其从文档中完全排除。对于半透明图像,它们将作为 PostScript 图像蒙版添加。
以下示例演示了如何添加完全不透明的图像。“使用透明度”一文中将介绍如何添加透明图像。
要使用 Aspose.Page for Python via .NET 库将图像添加到新的 PsDocument,请按照示例中概述的步骤操作:
- 为生成的 PS 文件设置输出流。
- 使用默认选项实例化 PsSaveOptions 对象。
- 使用输出流和保存选项创建单页 PsDocument。
- 创建新的图形状态。
- 从图像文件创建 aspose.pydrawing.Bitmap。
- 为图像创建必要的转换。
- 将图像添加到 PsDocument 对象。
- 从当前图形状态退出到上一级图形状态。
- 关闭页面。
- 保存文档。
1# The path to the documents directory.
2data_dir = Util.get_data_dir_working_with_images()
3
4# Create an output stream for the PostScript document
5with open(data_dir + "AddImage_outPS.ps", "wb") as out_ps_stream:
6 # Create the save options with A4 size
7 options = PsSaveOptions()
8
9 # Create a new 1-paged PS Document
10 document = PsDocument(out_ps_stream, options, False)
11
12
13 document.write_graphics_save()
14 document.translate(100, 100)
15
16 # Create a Bitmap object from the image file
17 with aspose.pydrawing.Bitmap(data_dir + "TestImage Format24bppRgb.jpg") as image:
18 # Create an image transform
19 transform = aspose.pydrawing.drawing2d.Matrix()
20 transform.translate(float(35), float(300))
21 transform.scale(float(3), float(3))
22 transform.rotate(float(-45))
23
24 # Add the image to the document
25 document.draw_image(image, transform, aspose.pydrawing.Color())
26
27 document.write_graphics_restore()
28
29 # Close the current page
30 document.close_page()
31
32 # Save the document
33 document.save()
运行此代码的结果是:
您可以从 GitHub 下载示例和数据文件。