在 PS 文件中使用透明度 | Python
在 PS 文档中添加透明度
由于 PostScript 本身不支持在绘制矢量图形对象时使用透明度,因此在 PS 文档中添加透明度颇具挑战性。然而,半透明图像可以通过完全透明和完全不透明像素的组合来表示,这些像素通常被称为蒙版。
Aspose.Page for Python via .NET 提供了一种向 PS 文档添加透明图像的方法。对于绘制形状或文本等矢量图形,我们采用一种称为“伪透明”的技术。该技术通过将红、绿、蓝三色的 Alpha 值混合,使 Alpha 值小于 255 的颜色变亮。虽然“伪透明”并不能在透明图层下方提供真正的图层可见性,但它会营造出透明的视觉效果,尤其是在底层为白色时。
向 PS 文档添加透明图像
如前所述,Aspose.Page for Python via .NET 库提供了 add_transparent_image() 方法,用于向 PS 文档添加透明图像。此方法可辨别图像是完全不透明、完全透明还是半透明。如果图像完全不透明,则使用 add_image() 方法添加。如果图像完全透明,则根本不添加。对于半透明图像,则将其作为 PostScript 图像蒙版添加。
在下面的示例中,我们演示了使用 add_image() 和 add_transparent_image() 在 PS 文档中添加透明图像的区别。为了使白色半透明图像可视化,我们在图像下方放置了一个大的红色矩形。
要使用 Aspose.Page for Python 通过 .NET 库将图像添加到新的 PsDocument,请按照以下步骤操作:
- 为生成的 PS 文件创建输出流。
- 使用默认选项创建一个 PsSaveOptions 对象。
- 使用已创建的输出流和保存选项创建一个单页 PsDocument。
- 创建新的图形状态。
- 从图像文件创建 aspose.pydrawing.Bitmap。
- 为图像创建必要的转换。
- 如果我们确定图像不透明,则将其作为完全不透明图像(使用 add_image() 方法)添加到 PsDocument;如果我们不确定图像不透明,则将其作为透明图像(使用 add_transparent_image() 方法)添加。
- 从当前图形状态退出到上一级图形状态。
- 关闭页面。
- 保存文档。
1# The path to the documents directory.
2data_dir = Util.get_data_dir_working_with_transparency()
3
4# Create an output stream for the PostScript document
5with open(data_dir + "AddTransparentImage_outPS.ps", "wb") as out_ps_stream:
6 # Create the save options with the A4 size
7 options = PsSaveOptions()
8 # Set the page's background color to see a white image on it's own transparent background
9 options.background_color = aspose.pydrawing.Color.from_argb(211, 8, 48)
10
11 # Create a new 1-paged PS Document
12 document = PsDocument(out_ps_stream, options, False)
13
14
15 document.write_graphics_save()
16 document.translate(20, 100)
17
18 # Create a bitmap from the translucent image file
19 with aspose.pydrawing.Bitmap(data_dir + "mask1.png") as image:
20 # Add this image to the document as a regular opaque RGB image
21 document.draw_image(image, aspose.pydrawing.drawing2d.Matrix(1., 0., 0., 1., 100., 0.), aspose.pydrawing.Color())
22
23 # Create another bitmap from the same image file
24 with aspose.pydrawing.Bitmap(data_dir + "mask1.png") as image:
25 # Add this image to the document as a transparent image
26 document.draw_transparent_image(image, aspose.pydrawing.drawing2d.Matrix(1., 0., 0., 1., 350., 0.), 255)
27
28 document.write_graphics_restore()
29
30 # Close the current page
31 document.close_page()
32
33 #Save the document
34 document.save()
运行此代码的结果是:
添加透明矢量图形对象
之前我们提到,此 API 解决方案使用一种针对透明形状和文本的渐变算法,我们称之为**“伪透明”**。 在下一个示例中,我们将演示两个使用相同颜色绘制的形状之间的区别,但第一个形状没有 Alpha 分量,而第二个形状有 Alpha 分量。
1# The path to the documents directory.
2data_dir = Util.get_data_dir_working_with_transparency()
3
4# Create an output stream for the PostScript document
5with open(data_dir + "ShowPseudoTransparency_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 offset_x = 50.
13 offset_y = 100.
14 width = 200.
15 height = 100.
16
17 ################################ Create a rectangle with the opaque gradient fill #######################################################
18 path = aspose.pydrawing.drawing2d.GraphicsPath()
19 path.add_rectangle(aspose.pydrawing.RectangleF(offset_x, offset_y, width, height))
20
21 opaque_brush: aspose.pydrawing.drawing2d.LinearGradientBrush = \
22 GraphicsFactory.create_linear_gradient_brush_by_rect_and_angle(aspose.pydrawing.RectangleF(0, 0, 200, 100), aspose.pydrawing.Color.from_argb(0, 0, 0),
23 aspose.pydrawing.Color.from_argb(40, 128, 70), 0)
24 brush_transform = aspose.pydrawing.drawing2d.Matrix(width, 0., 0., height, offset_x, offset_y)
25 opaque_brush.transform = brush_transform
26 gradient_brush = GradientBrush(opaque_brush)
27 gradient_brush.wrap_mode = aspose.pydrawing.drawing2d.WrapMode.CLAMP
28
29 document.set_paint(gradient_brush)
30 document.fill(path)
31 ####################################################################################################################################
32
33 offset_x = 350.
34
35 ################################ Create a rectangle with the translucent gradient fill ###################################################
36 # Create a graphics path from the first rectangle
37 path = aspose.pydrawing.drawing2d.GraphicsPath()
38 path.add_rectangle(aspose.pydrawing.RectangleF(offset_x, offset_y, width, height))
39
40 # Create linear gradient brush colors which transparency are not 255, but 150 and 50. So it are translucent.
41 translucent_brush: aspose.pydrawing.drawing2d.LinearGradientBrush = \
42 GraphicsFactory.create_linear_gradient_brush_by_rect_and_angle(aspose.pydrawing.RectangleF(0, 0, width, height),
43 aspose.pydrawing.Color.from_argb(150, 0, 0, 0),
44 aspose.pydrawing.Color.from_argb(50, 40, 128, 70), 0)
45 # Create a transform for brush.
46 brush_transform = aspose.pydrawing.drawing2d.Matrix(width, 0., 0., height, offset_x, offset_y)
47 # Set the transform
48 translucent_brush.transform = brush_transform
49 # Create a GradientBrush object containing the linear gradient brush
50 gradient_brush = GradientBrush(translucent_brush)
51 gradient_brush.wrap_mode = aspose.pydrawing.drawing2d.WrapMode.CLAMP
52 # Set the paint
53 document.set_paint(gradient_brush)
54 # Fill the rectangle
55 document.fill(path)
56 ####################################################################################################################################
57
58 # Close the current page
59 document.close_page()
60
61 # Save the document
62 document.save()
运行此代码的结果是:
您可以从 GitHub下载示例和数据文件。