如何使用python枕头在图像上添加段落,并将文本居中对齐?
我想在图像上写文本,文本很长,不能在图像上打印所有文本 所以我想把文本放在中间,当图像的大小超出图像的范围时,在图像上一行一行地写一个段落如何使用python枕头在图像上添加段落,并将文本居中对齐?,python,image-processing,python-imaging-library,Python,Image Processing,Python Imaging Library,我想在图像上写文本,文本很长,不能在图像上打印所有文本 所以我想把文本放在中间,当图像的大小超出图像的范围时,在图像上一行一行地写一个段落 from PIL import Image from PIL import ImageFont from PIL import ImageDraw import textwrap with open(".txt", "r") as f: W, H = (500,500) for line in f: singleline =
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
import textwrap
with open(".txt", "r") as f:
W, H = (500,500)
for line in f:
singleline = (line)
img = Image.open("bb1.jpg")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype('demo2.ttf',20)
l = textwrap.wrap(singleline, width=40)
w, h = draw.textsize(singleline)
draw.text(((W-w)/2,(H-h)/2),singleline,font=font, fill = 'black')
img.save(singleline+'.jpg')
这是我作为输出得到的图像
文本不在中心,也不是所有文本都显示在图像上,而是在图像之外