Working with Images in PS file | Python

Contents
[ Hide Show ]

Add Image in PS Document

Aspose.Page for Python via .NET library provides two approaches for incorporating images into a PS document:

This distinction arises because PostScript does not inherently support transparency. However, translucent images can be represented as a combination of fully transparent and fully opaque pixels, known as masks. When adding a translucent image to a PS document, it’s essential to perform checks and preprocessing to ensure the transparency is accurately reflected. This process requires additional time. Therefore, if the image is known to be fully opaque, it’s more efficient to utilize the first method to save execution time.

The second method automatically determines whether the image is fully opaque, fully transparent, or translucent. If the image is fully opaque, it’s added using the first method. If it’s fully transparent, it’s excluded from the document altogether. For translucent images, they are added as PostScript image masks.

The example below demonstrates how to add a fully opaque image. Adding a transparent image will be illustrated in the “Working with Transparency” article.

In order to add an image to a new PsDocument using the Aspose.Page for Python via .NET library, follow these steps outlined in the example:

  1. Set up an output stream for the resulting PS file.
  2. Instantiate a PsSaveOptions object with default options.
  3. Create a one-paged PsDocument using the output stream and save options.
  4. Create a new graphics state.
  5. Create aspose.pydrawing.Bitmap from the image file.
  6. Create the necessary transformation for the image.
  7. Add the image to the PsDocument object.
  8. Exit from the current graphics state to upper level one.
  9. Close the page.
  10. Save the document.
 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()

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

The result of running this code is

Add Image

You can download examples and data files from GitHub.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.