2 回答
TA贡献1784条经验 获得超2个赞
我完美地修复了它。请测试一下。
import os
from PIL import Image, ImageDraw, ImageFont
def text_on_img(filename='01.png', text="Text to write on \n img 1234567890", size=200, color=(0, 0, 0), bg='white'):
"Draw a text on an Image, saves it, show it"
fnt = ImageFont.truetype('arial.ttf', size)
# create image
width = max([int(size/2) * len(line) for line in text.split('\n')])
height = (size + 50) * len(text.split('\n'))
image = Image.new(mode="RGB", size=(width, height), color=bg)
draw = ImageDraw.Draw(image)
# draw text
draw.text((10, 10), text, font=fnt, fill=(0, 0, 0))
# save file
image.save(filename)
# show file
os.system(filename)
text_on_img()
结果:
TA贡献1836条经验 获得超4个赞
一种方法是乘以size
出现的次数\n
。
n = text.count('\n') + 2# create imageimage = Image.new(mode = "RGB", size = (int(size/2)*len(text),size*n), color = bg)
结果:
如果从文本中删除\n
,结果将是:
其他例子:
添加回答
举报