我正在尝试用 Python 将文本转换为图像
这是代码:只要文本是单行,
例如“要在 img 1234567890 上写入的文本”
没事
但如果文本包含“\n”,则图像剪辑和尺寸计算将变得不正确
“要写在 \n img 1234567890 上的文本”
请帮忙
import numpy as np
import time
import text_to_image
from PIL import Image, ImageDraw, ImageFont
import os
from win32api import GetSystemMetrics
def text_on_img(filename='01.png', text="Text to write on \n img 1234567890", size=200, color=(0,0,0), bg='white'):
"Draw a text on an Image, saves it, show it"
fnt = ImageFont.truetype('arial.ttf', size)
# create image
image = Image.new(mode = "RGB", size = (int(size/2)*len(text),size+50), color = bg)
draw = ImageDraw.Draw(image)
# draw text
draw.text((10,10), text, font=fnt, fill=(0,0,0))
# save file
image.save(filename)
# show file
os.system(filename)
text_on_img()
一只斗牛犬
HUH函数
相关分类