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.
 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_images()
11
12# Create an output stream for the PostScript document
13with open(data_dir + "AddImage_outPS.ps", "wb") as out_ps_stream:
14    # Create the save options with A4 size
15    options = PsSaveOptions()
16    
17    # Create a new 1-paged PS Document
18    document = PsDocument(out_ps_stream, options, False)
19    
20    
21    document.write_graphics_save()
22    document.translate(100, 100)
23    
24    # Create a Bitmap object from the image file
25    with aspose.pydrawing.Bitmap(data_dir + "TestImage Format24bppRgb.jpg") as image:
26        # Create an image transform
27        transform = aspose.pydrawing.drawing2d.Matrix()
28        transform.translate(float(35), float(300))
29        transform.scale(float(3), float(3))
30        transform.rotate(float(-45))
31        
32        # Add the image to the document
33        document.draw_image(image, transform, aspose.pydrawing.Color())
34    
35    document.write_graphics_restore()
36    
37    # Close the current page
38    document.close_page()
39    
40    # Save the document
41    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.

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.