小编use*_*541的帖子

python PIL在图像上绘制多行文字

我尝试在图像的底部添加文本,实际上我已经完成了,但是如果我的文本比图像宽度长,则从两侧剪切,以简化我希望文本在多行中,如果它是比图像宽度长.这是我的代码:

FOREGROUND = (255, 255, 255)
WIDTH = 375
HEIGHT = 50
TEXT = 'Chyba najwy?szy czas zada? to pytanie na ?niadanie \n Chyba najwy?szy czas zada? to pytanie na ?niadanie'
font_path = '/Library/Fonts/Arial.ttf'
font = ImageFont.truetype(font_path, 14, encoding='unic')
text = TEXT.decode('utf-8')
(width, height) = font.getsize(text)

x = Image.open('media/converty/image.png')
y = ImageOps.expand(x,border=2,fill='white')
y = ImageOps.expand(y,border=30,fill='black')

w, h = y.size
bg = Image.new('RGBA', (w, 1000), "#000000")

W, H = bg.size
xo, yo = (W-w)/2, (H-h)/2
bg.paste(y, (xo, 0, xo+w, h))
draw …
Run Code Online (Sandbox Code Playgroud)

python text image python-imaging-library

32
推荐指数
4
解决办法
2万
查看次数

标签 统计

image ×1

python ×1

python-imaging-library ×1

text ×1