依赖
C:WindowsFontsmicross.ttf
PILLOW
from PIL import Image, ImageDraw, ImageFont
class ImgText:
font = ImageFont.truetype("micross.ttf", 24)
def __init__(self, text):
# 预设宽度 可以修改成你需要的图片宽度
self.width = 100
# 文本
self.text = text
# 段落 , 行数, 行高
self.duanluo, self.note_height, self.line_height = self.split_text()
def get_duanluo(self, text):
txt = Image.new('RGBA', (100, 100), (255, 255, 255, 0))
draw = ImageDraw.Draw(txt)
# 所有文字的段落
duanluo = ""
# 宽度总和
sum_width = 0
# 几行
line_count = 1
# 行高
line_height = 0
for char in text:
width, height = draw.textsize(char, ImgText.font)
sum_width += width
if sum_width > self.width: # 超过预设宽度就修改段落 以及当前行数