Python Pillow - ImageDraw.multiline_text() 函式



Pillow 庫中的 ImageDraw.multiline_text() 方法用於在影像上的指定位置繪製多行文字。

語法

以下是函式的語法:

ImageDraw.multiline_text(xy, text, fill=None, font=None, anchor=None, spacing=4, align='left', direction=None, features=None, language=None, stroke_width=0, stroke_fill=None, embedded_color=False, font_size=None)

引數

以下是此函式引數的詳細資訊:

  • xy - 文字的錨點座標。它指定錨點與文字對齊的位置。

  • text - 要繪製的字串,包含多行文字。

  • fill - 用於文字的顏色。

  • font - 一個 ImageFont 例項,表示用於文字的字型。

  • anchor - 文字錨點對齊方式。它確定錨點相對於文字的位置。預設對齊方式為左上角。

  • spacing - 行之間的畫素數。

  • align - 多行文字中行的對齊方式。可以是“left”(左對齊)、“center”(居中對齊)或“right”(右對齊)。

  • direction - 文字的方向。可以是“rtl”(從右到左)、“ltr”(從左到右)或“ttb”(從上到下)。

  • features - 在文字佈局期間要使用的 OpenType 字型功能列表。它用於啟用或停用可選字型功能。

  • language - 文字的語言。它應該是 BCP 47 語言程式碼。

  • stroke_width - 文字筆劃的寬度。

  • stroke_fill - 用於文字筆劃的顏色。如果未給出,則預設為 fill 引數。

  • embedded_color - 是否使用字型嵌入顏色字形 (COLR、CBDT、SBIX)。

  • font_size - 如果未提供 font 引數,則指定用於預設字型的尺寸。

示例

示例 1

在此示例中,multiline_text() 方法用於使用預設引數在影像上繪製多行文字。

from PIL import Image, ImageDraw, ImageFont

# Create an image
image = Image.new("RGB", (700, 300), "white")

# Get a drawing context
draw = ImageDraw.Draw(image)

# Define the multiline text
text = "Hello,\nThis is\nMultiline Text!"

# Set the fill color for the text
fill_color = 'green'

# Draw multiline text on the image with default parameters
draw.multiline_text((150, 130), text, fill=fill_color)

# Display the image
image.show()
print('The multiline text is drawn successfully...')

輸出

The multiline text is drawn successfully...

輸出影像

multiline text

示例 2

在此示例中,多行文字使用不同的引數繪製在影像上。

from PIL import Image, ImageDraw, ImageFont

# Create a new image with a white background
image = Image.new("RGB", (700, 300), "black")
draw = ImageDraw.Draw(image)

# Define the multiline text
text = "Hello,\nThis is\nMultiline Text!"

# Set the font and size
font = ImageFont.truetype("arial.ttf", size=20)

# Set fill color for the text
fill_color = "orange"

# Draw multiline text on the image
draw.multiline_text((250, 100), text, fill=fill_color, font=font, spacing=10, align="left")

# Display the image
image.show()
print('The multiline text is drawn successfully...')

輸出

The multiline text is drawn successfully...

輸出影像

multiline text black background

示例 3

以下示例演示瞭如何在現有影像上使用不同的引數繪製多行文字。

from PIL import Image, ImageDraw, ImageFont

# Open an Image
image = Image.open('Images/TP-W.jpg')

# Create the draw object
draw = ImageDraw.Draw(image)

# Define the multiline text
multiline_text = "Tutorialspoint,\nSimple Easy Learning At Your Fingertips"

# Set the font and size
font = ImageFont.load_default()

# Set fill color for the text
fill_color = "darkgreen"

# Draw multiline text on the image
draw.multiline_text((240, 310), multiline_text, fill=fill_color, font=font, spacing=10, align="left")

# Display the image
image.show()

print('Multiline text is drawn successfully...')

輸出

Multiline text is drawn successfully...
draw multiline text
python_pillow_function_reference.htm
廣告