Working with Transparency in PS file | Python
Add transparency in PS document
Adding transparency to PS documents is pretty challenging since PostScript doesn’t inherently support transparency in painting vector graphics objects. However, translucent images can be represented by a combination of fully transparent and fully opaque pixels, often referred to as masks.
Aspose.Page for Python via .NET provides a method for adding transparent images to PS documents. For painting vector graphics like shapes or text, we employ a technique known as “pseudo-transparency”. This involves lightening colors with an Alpha component of less than 255 by blending the Red, Green, and Blue components with an Alpha of one. While “pseudo-transparency” doesn’t offer true layer visibility beneath transparent layers, it creates the illusion of transparency, especially when the bottom layer is white.
Add transparent image to PS document
As mentioned earlier, Aspose.Page for Python via .NET library offers the add_transparent_image() method for adding transparent images to PS documents. This method discerns whether the image is fully opaque, fully transparent, or translucent. If the image is fully opaque, it is added using the add_image() method. If it’s fully transparent, it’s not added at all. For translucent images, it’s added as a PostScript image mask.
In the example below, we illustrate the distinction between adding a transparent image in a PS document using both add_image() and add_transparent_image(). To visualize the white translucent image, we’ve placed a large red rectangle beneath the images.
To add an image to a new PsDocument using Aspose.Page for Python via .NET library, we follow these steps:
- Create an output stream for the resulting PS file.
- Create a PsSaveOptions object with the default options.
- Create a 1-paged PsDocument with an already created output stream and save options.
- Create a new graphics state.
- Create aspose.pydrawing.Bitmap from an image file.
- Create the necessary transformation for the image.
- Add the image to PsDocument as a fully opaque image (using add_image() method) if we are sure that the image is opaque or add one as a transparent image (using add_transparent_image() method) if we are not sure that the image is opaque.
- Exit from the current graphics state to the upper level one.
- Close the page.
- Save the document.
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()
The result of running this code is
Adding transparent vector graphics object
Earlier we wrote that this API Solution uses a paling algorithm for transparent shapes and text, which we called “pseudo-transparency”. In the next example we demonstrate a difference between two shapes painted with the same color, but in the first shape it is without the Alpha component and in the second case it has tje component.
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()
The result of running this code is
You can download examples and data files from GitHub.