画像の操作 | Python

PSドキュメントに画像を追加する

Aspose.Page for Python の .NET ライブラリは、PSドキュメントに画像を組み込むための2つの方法を提供します。

この区別は、PostScript が本質的に透明をサポートしていないために生じます。ただし、半透明画像は、マスク と呼ばれる完全に透明なピクセルと完全に不透明なピクセルの組み合わせとして表現できます。PSドキュメントに半透明画像を追加する場合、透明度が正確に反映されていることを確認するためのチェックと前処理を実行することが不可欠です。この処理には追加の時間がかかります。したがって、画像が完全に不透明であることが分かっている場合は、実行時間を節約するために最初の方法を使用する方が効率的です。

2つ目の方法は、画像が完全に不透明、完全に透明、または半透明であるかを自動的に判別します。画像が完全に不透明の場合は、1つ目の方法で追加されます。完全に透明な場合は、ドキュメントから完全に除外されます。半透明の画像は、PostScript画像マスクとして追加されます。

以下の例は、完全に不透明な画像を追加する方法を示しています。透明な画像の追加方法については、「透明部分の操作」の記事で説明します。

Aspose.Page for Python via .NETライブラリを使用して新しい PsDocumentに画像を追加するには、例に示されている以下の手順に従ってください。

  1. 結果の PS ファイルの出力ストリームを設定します。
  2. デフォルトのオプションで PsSaveOptions オブジェクトをインスタンス化します。
  3. 出力ストリームと保存オプションを使用して、1 ページの PsDocument を作成します。
  4. 新しいグラフィックス状態を作成します。
  5. 画像ファイルから aspose.pydrawing.Bitmap を作成します。
  6. 画像に必要な変換を作成します。
  7. 画像を PsDocument オブジェクトに追加します。
  8. 現在のグラフィックス状態から上位レベルのグラフィックス状態に移行します。
  9. ページを閉じます。
  10. ドキュメントを保存します。
 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()

.NETJava の PS ドキュメント内の画像の操作を参照してください。

このコードを実行した結果は

Add Image

サンプルとデータ ファイルは GitHub からダウンロードできます。

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.