修复Python线串

问题描述 投票:0回答:1

没有行间距,行与字母合并。

from PIL import Image, ImageDraw, ImageFont

image1 = Image.open('pic/input_image.jpg')
image2 = Image.open('pic/background_image.jpg')
newphoto = image1.resize((109, 150))
image2.paste(newphoto, (10, 14))
draw = ImageDraw.Draw(image2)
font = ImageFont.truetype('arial.ttf', 16)
text = "Ivanov Igor Stepanovich"
columns = 3
words = text.split()
column_width = 150 // columns
column_height = 300
x = 155
y = 100
for word in words:
    # Расчет размеров текста
    bbox = draw.textbbox((0, 0), text, font=font)
    text_width = bbox[2] - bbox[0]
    text_height = bbox[3] - bbox[1]
    if y + text_height > column_height:
        x += column_width
        y = 100  # Изменил начальное значение y
    draw.text((x, y), word, fill=(0, 0, 0), font=font)
    y += text_height
image2.save('pic/result_image.jpg')

预期:用行距分隔行中的文本

python python-imaging-library
1个回答
0
投票

您正在将

text_height
设置为文本的边界框。在此值上添加几个点以提供行间距:

y += 文本高度

© www.soinside.com 2019 - 2024. All rights reserved.