透明度の設定 | Python
PSドキュメントに透明部分を追加
PostScriptはベクターグラフィックオブジェクトの描画において透明部分をサポートしていないため、PSドキュメントに透明部分を追加するのは非常に困難です。しかし、半透明の画像は、完全に透明なピクセルと完全に不透明なピクセル(マスクと呼ばれる)の組み合わせで表現できます。
Aspose.Page for Python via .NETは、PSドキュメントに透明画像を追加する方法を提供します。図形やテキストなどのベクターグラフィックを描画する際には、**「擬似透明」と呼ばれる手法を採用しています。これは、アルファ値が255未満の色を、アルファ値が1の赤、緑、青の3つの要素をブレンドすることで明るくする手法です。「擬似透明」**では透明レイヤーの下にあるレイヤーが実際に見えるわけではありませんが、特に下層が白の場合に透明であるような錯覚を作り出すことができます。
PSドキュメントに透明画像を追加する
前述の通り、Aspose.Page for Python の .NET ライブラリには、PSドキュメントに透明画像を追加するための add_transparent_image() メソッドが用意されています。このメソッドは、画像が完全に不透明、完全に透明、または半透明であるかを識別します。画像が完全に不透明の場合は、add_image() メソッドを使用して追加されます。完全に透明な場合は、画像はまったく追加されません。半透明の画像の場合は、PostScript 画像マスクとして追加されます。
以下の例では、add_image() と add_transparent_image() の両方を使用して PSドキュメントに透明画像を追加する場合の違いを示しています。白い半透明画像を視覚化するために、画像の下に大きな赤い四角形を配置しています。
.NET ライブラリ経由で Aspose.Page for Python を使用して新しい PsDocument に画像を追加するには、次の手順に従います。
- 生成された PS ファイルの出力ストリームを作成します。
- デフォルトのオプションを使用して PsSaveOptions オブジェクトを作成します。
- 既に作成済みの出力ストリームと保存オプションを使用して、1 ページの PsDocument を作成します。
- 新しいグラフィックス状態を作成します。
- 画像ファイルから aspose.pydrawing.Bitmap を作成します。
- 画像に必要な変換を作成します。
- 画像が不透明であることが確実な場合は、完全に不透明な画像として PsDocument に追加します (add_image() メソッドを使用)。不透明であることが確実でない場合は、透明な画像として追加します (add_transparent_image() メソッドを使用)。
- 現在のグラフィック状態から上位レベルの状態に移行します。
- ページを閉じます。
- ドキュメントを保存します。
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()
このコードを実行した結果は
透明なベクターグラフィックオブジェクトの追加
先ほど、このAPIソリューションでは透明な図形とテキストに「疑似透明」と呼ばれる淡色化アルゴリズムを使用していると説明しました。 次の例では、同じ色で塗りつぶされた2つの図形の違いを示します。最初の図形にはアルファ成分がなく、2番目の図形にはアルファ成分があります。
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()
このコードを実行した結果は
サンプルとデータ ファイルは GitHub からダウンロードできます。