如何使用Pillow Package在图像文件上绘制段落



我是枕头包的新手,我正在尝试绘制一段居中对齐的大约100个单词的文本,但文本超出了图像,我该如何管理它,下面是我尝试的代码:

请告诉我

谢谢

def certgenerator(name,course,datetext,cid):
"""function to generate certificate"""
image_source = Image.open(r'/media/xxxxx/xxxxxxx/certgentestroom/certgen-1/originals/empty002.jpg')
spacing = 50
draw = ImageDraw.Draw(image_source)
x = datetime.now()
msg3  = x.strftime("%d.%m.%Y")
font = ImageFont.truetype("/media/xxxxxx/xxxxx/certgentestroom/fonts/ArialBold.ttf", size=60)
draw.multiline_text((750,730), msg3, fill ="black", font = font,spacing = spacing,align="center")
name_width, name_height = draw.textsize(name, font=font)
# draw.text((WIDTH-name_width/2, HEIGHT-name_height/2), name, fill=FONT_COLOR, font=font)
draw.text((750,850), name, fill=FONT_COLOR, font=font)
font = ImageFont.truetype("/media/xxxxxxx/xxxxxxxxxxx/certgentestroom/fonts/ArialBold.ttf", size=60)
name_width, name_height = draw.textsize(course, font=font)
# draw.text((CWIDTH-name_width/2, CHEIGHT-name_height/2), course, fill=FONT_COLOR, font=font)
draw.text((750,1100), course, fill=FONT_COLOR, font=font)
#Date generation module
#draw.multiline_text((2520,2490), msg3, fill ="black", font = font,spacing = spacing,align="center")    
z=1220
x =0
y = len(description)
for i in range(x,y,12):
x = i
print("no.of words:",len(description[x:x+12]))
print (description[x:x+12])
j = ' '.join(description[x:x+12])
draw.multiline_text((755,z), j, fill ="black", font = font,spacing = spacing,align="center")
z=z+80

# font = ImageFont.truetype("/media/mgbhargava/Codegnan/certgentestroom/fonts/arial.ttf", size=30)
# draw.multiline_text((740,1220), decsription, fill ="black", font = font,spacing = spacing,align="center")    
image_source.save( name+".jpg")
print('printing certificate of: '+name)

是的,我试着在空背景上打印段落,并在所需的图像上渲染,感谢的帮助

最新更新