Python PIL | ImageDraw.Draw.multiline_textsize()
PIL 是Python Imaging Library ,它为Python解释器提供了图像编辑功能。 ImageDraw
模块为 Image 对象提供简单的 2D 图形。您可以使用此模块创建新图像、注释或修饰现有图像,以及动态生成图形以供 Web 使用。
ImageDraw.Draw.multiline_textsize()
返回给定字符串的大小,以像素为单位。
Syntax:
ImageDraw.Draw.multiline_textsize(text, font=None, spacing=0)
Parameters:
text – Text to be measured.
font – An ImageFont instance.
spacing – The number of pixels between lines.
Return Type:
returns an image with text.
使用的图像:
代码:使用 ImageDraw.Draw.multiline_textsize
# Importing Image and ImageFont, ImageDraw module from PIL package
from PIL import Image, ImageFont, ImageDraw
# creating a image object
image = Image.open(r'C:\Users\System-Pc\Desktop\rose.jpg')
draw = ImageDraw.Draw(image)
#specified font size
font = ImageFont.truetype(r'C:\Users\System-Pc\Desktop\arial.ttf',30)
text =u"""\
ALWAYS BE HAPPY
(LAUGHING IS THE \n BEST MEDICINE)"""
# drawing text size
draw.text((20,18), text,font = None,spacing=0)
image.show()
输出: