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:

  1. Create an output stream for the resulting PS file.
  2. Create a PsSaveOptions object with the default options.
  3. Create a 1-paged PsDocument with an already created output stream and save options.
  4. Create a new graphics state.
  5. Create aspose.pydrawing.Bitmap from an image file.
  6. Create the necessary transformation for the image.
  7. 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.
  8. Exit from the current graphics state to the upper level one.
  9. Close the page.
  10. Save the document.
 1from aspose.page.eps import *
 2from aspose.page.eps.device import *
 3import aspose.pydrawing
 4from util import Util
 5###############################################
 6###### Class and Method declaration here ######
 7###############################################
 8
 9# The path to the documents directory.
10data_dir = Util.get_data_dir_working_with_transparency()
11
12# Create an output stream for the PostScript document
13with open(data_dir + "AddTransparentImage_outPS.ps", "wb") as out_ps_stream:
14    # Create the save options with the A4 size
15    options = PsSaveOptions()
16    # Set the page's background color to see a white image on it's own transparent background
17    options.background_color = aspose.pydrawing.Color.from_argb(211, 8, 48)
18    
19    # Create a new 1-paged PS Document
20    document = PsDocument(out_ps_stream, options, False)
21    
22    
23    document.write_graphics_save()
24    document.translate(20, 100)
25    
26    # Create a bitmap from the translucent image file
27    with aspose.pydrawing.Bitmap(data_dir + "mask1.png") as image:
28        # Add this image to the document as a regular opaque RGB image
29        document.draw_image(image, aspose.pydrawing.drawing2d.Matrix(1., 0., 0., 1., 100., 0.), aspose.pydrawing.Color())
30    
31    # Create another bitmap from the same image file
32    with aspose.pydrawing.Bitmap(data_dir + "mask1.png") as image:
33        # Add this image to the document as a transparent image
34        document.draw_transparent_image(image, aspose.pydrawing.drawing2d.Matrix(1., 0., 0., 1., 350., 0.), 255)
35    
36    document.write_graphics_restore()
37    
38    # Close the current page
39    document.close_page()
40    
41    #Save the document
42    document.save()

See working with transparency in PS document in .NET, Java.

The result of running this code is

Add Transparent Image

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.

 1from aspose.page import *
 2from aspose.page.eps import *
 3from aspose.page.eps.device import *
 4import aspose.pydrawing
 5from util import Util
 6###############################################
 7###### Class and Method declaration here ######
 8###############################################
 9
10# The path to the documents directory.
11data_dir = Util.get_data_dir_working_with_transparency()
12
13# Create an output stream for the PostScript document
14with open(data_dir + "ShowPseudoTransparency_outPS.ps", "wb") as out_ps_stream:
15    # Create the save options with A4 size
16    options = PsSaveOptions()
17    
18    # Create a new 1-paged PS Document
19    document = PsDocument(out_ps_stream, options, False)
20    
21    offset_x = 50.
22    offset_y = 100.
23    width = 200.
24    height = 100.
25    
26    ################################ Create a rectangle with the opaque gradient fill #######################################################
27    path = aspose.pydrawing.drawing2d.GraphicsPath()
28    path.add_rectangle(aspose.pydrawing.RectangleF(offset_x, offset_y, width, height))
29    
30    opaque_brush: aspose.pydrawing.drawing2d.LinearGradientBrush = \
31        GraphicsFactory.create_linear_gradient_brush_by_rect_and_angle(aspose.pydrawing.RectangleF(0, 0, 200, 100), aspose.pydrawing.Color.from_argb(0, 0, 0),
32    aspose.pydrawing.Color.from_argb(40, 128, 70), 0)
33    brush_transform = aspose.pydrawing.drawing2d.Matrix(width, 0., 0., height, offset_x, offset_y)
34    opaque_brush.transform = brush_transform
35    gradient_brush = GradientBrush(opaque_brush)
36    gradient_brush.wrap_mode = aspose.pydrawing.drawing2d.WrapMode.CLAMP
37    
38    document.set_paint(gradient_brush)
39    document.fill(path)
40    ####################################################################################################################################
41    
42    offset_x = 350.
43    
44    ################################ Create a rectangle with the translucent gradient fill ###################################################
45    # Create a graphics path from the first rectangle
46    path = aspose.pydrawing.drawing2d.GraphicsPath()
47    path.add_rectangle(aspose.pydrawing.RectangleF(offset_x, offset_y, width, height))
48    
49    # Create linear gradient brush colors which transparency are not 255, but 150 and 50. So it are translucent.
50    translucent_brush: aspose.pydrawing.drawing2d.LinearGradientBrush = \
51        GraphicsFactory.create_linear_gradient_brush_by_rect_and_angle(aspose.pydrawing.RectangleF(0, 0, width, height),
52                                                                       aspose.pydrawing.Color.from_argb(150, 0, 0, 0),
53    aspose.pydrawing.Color.from_argb(50, 40, 128, 70), 0)
54    # Create a transform for brush.
55    brush_transform = aspose.pydrawing.drawing2d.Matrix(width, 0., 0., height, offset_x, offset_y)
56    # Set the transform
57    translucent_brush.transform = brush_transform
58    # Create a GradientBrush object containing the linear gradient brush
59    gradient_brush = GradientBrush(translucent_brush)
60    gradient_brush.wrap_mode = aspose.pydrawing.drawing2d.WrapMode.CLAMP
61    # Set the paint
62    document.set_paint(gradient_brush)
63    # Fill the rectangle
64    document.fill(path)
65    ####################################################################################################################################
66    
67    # Close the current page
68    document.close_page()
69    
70    # Save the document
71    document.save()

See working with transparency in PS documents in .NET, Java.

The result of running this code is

Show Pseudo Transparency

You can download examples and data files from GitHub.

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.