Python 将字符串传输到图像时编辑字体属性

Python 将字符串传输到图像时编辑字体属性,python,python-3.x,image,jupyter-notebook,python-imaging-library,Python,Python 3.x,Image,Jupyter Notebook,Python Imaging Library,我一直在使用Python3和Jupyter笔记本中的Pillow来获取用户输入的字符串,并将其转换为图像 一旦字符串成功更改为图像,图像的字体大小和样式仍然很小,并且在尝试编辑与文本宽度和高度关联的变量时不会更改,但边框空间确实会更改 例如,如果我放大文本的宽度或高度,字体不会改变,但字体周围的白色边框会改变。如果有人知道如何解决这个问题,请告诉我 from PIL import Image, ImageDraw, ImageFont font = ImageFont.truetype("Ve

我一直在使用Python3和Jupyter笔记本中的Pillow来获取用户输入的字符串,并将其转换为图像

一旦字符串成功更改为图像,图像的字体大小和样式仍然很小,并且在尝试编辑与文本宽度和高度关联的变量时不会更改,但边框空间确实会更改

例如,如果我放大文本的宽度或高度,字体不会改变,但字体周围的白色边框会改变。如果有人知道如何解决这个问题,请告诉我

from PIL import Image, ImageDraw, ImageFont

font = ImageFont.truetype("Verdana.ttf", 100) #seems to have no impact on the program

userinput0000 = input("Enter String Here:")
bgcolor = 'white'
txtcolor = 'black'
text_width, text_height = (5 , 5) # (w,h) changes the size of the white box surrounding the string

userinput0000img = Image.new("RGB",(text_width, text_height), bgcolor)
draw = ImageDraw.Draw(userinput0000img)
draw.text((5, 5), userinput0000, txtcolor) #(x,y)changes the xy coordinates of the string

userinput0000img.show()

im = Image.open("Desktop/0001.jpg")
x, y = userinput0000img.size
im.paste(userinput0000img,(0,0,x,y))
im.show()
当前代码输出:

我发现这两个堆栈溢出帖子很有用,但它们似乎都没有解决相同类型的问题

我也阅读了枕头手册(链接到下面),没有发现类似情况


您没有在调用中添加
font
参数,因此引用了您创建的字体实例

所以,改变

draw.text((5,5),userinput0000,txtcolor)
差不多

draw.text((5,5),userinput0000,txtcolor,font=font)
你完成了


希望有帮助

非常感谢,解决了这个问题!