【发布时间】:2020-06-08 17:53:11
【问题描述】:
我一直在 Jupyter Notebook 中使用 Python 3 和 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()
当前代码输出:
这是我发现有用的两篇 Stack Overflow 帖子,但似乎都没有解决同一类型的问题。
How I can load a font file with PIL.ImageFont.truetype without specifying the absolute path?
Convert HTML string to image data and display it as an image in a template using Python
我还阅读了枕头手册(链接如下),没有发现类似情况的参考。
https://pillow.readthedocs.io/en/latest/handbook/tutorial.html
【问题讨论】:
标签: python python-3.x image jupyter-notebook python-imaging-library