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
广告

© . All rights reserved.