📜  Python PIL |ImageDraw.Draw.multiline_text()

📅  最后修改于: 2022-05-13 01:55:11.823000             🧑  作者: Mango

Python PIL |ImageDraw.Draw.multiline_text()


PIL 是Python Imaging Library,它为Python解释器提供了图像编辑功能。 ImageDraw模块为 Image 对象提供简单的 2D 图形。您可以使用此模块创建新图像、注释或修饰现有图像,以及动态生成图形以供 Web 使用。

ImageDraw.Draw.multiline_text()在给定位置绘制字符串。

使用的图像:

代码:ImageDraw.Draw.multiline_text() 示例

# 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', 15) 
spacing = 50
text = u"""\
ALWAYS BE HAPPY
(LAUGHING IS THE \n BEST MEDICINE)"""
  
# drawing text size
draw.text((6, 8), text, fill ="red", font = font, 
          spacing = spacing, align ="right") 
  
image.show() 

输出:

另一个例子:这里我们改变参数。

使用的图像:

代码:ImageDraw.Draw.multiline_text() 示例

# 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\flower.jpg') 
  
draw = ImageDraw.Draw(image) 
  
# specified font size
font = ImageFont.truetype(r'C:\Users\System-Pc\Desktop\arial.ttf', 15) 
spacing = 50
text = u"""\
ALWAYS BE HAPPY
(LAUGHING IS THE \n BEST MEDICINE)"""
  
# drawing text size
draw.text((6, 8), text, fill ="black", 
          font = font, spacing = spacing, align ="right") 
  
image.show() 

输出: