如何用PIL绘制粗体/斜体文字? ImageFont.truetype(文件,大小)有一个选项来指定字体大小。

有帮助吗?

解决方案

使用字体的粗体/斜体

其他提示

许多字体使用不同的TTF文件作为粗体/斜体版本,所以我想如果您只指定该文件就可以使用。

如果(无论出于什么原因)你没有单独的粗体版本的字体,那么使字体变粗的一个相当hacky的解决方案是多次打印相同的文本并略微偏移。

andaleMono = ImageFont.truetype(ANDALE_MONO_PATH,16)
text = "hello world"
mainOffset = (50,50)
xoff, yoff = mainOffset
draw.text(mainOffset,text,font=andaleMono,fill='black')
draw.text((xoff+1,yoff+1),text,font=andaleMono,fill='black')
draw.text((xoff-1,yoff-1),text,font=andaleMono,fill='black')

嗯,这是我的第一个评论。我们走了。

我会尝试澄清这个程序。起初我做的是使用“名称”。像这样的字体

font = ImageFont.truetype("C:\Windows\Fonts\\Arial Negrita.ttf",25)

但只有这样的错误:

    Traceback (most recent call last):
  File "C:/Users/555STi/PycharmProjects/PIL/img.py", line 8, in <module>
    font = ImageFont.truetype("C:\Windows\Fonts\Arial negrita.ttf",25)
  File "C:\Python27\lib\site-packages\PIL\ImageFont.py", line 262, in truetype
    return FreeTypeFont(font, size, index, encoding)
  File "C:\Python27\lib\site-packages\PIL\ImageFont.py", line 142, in __init__
    self.font = core.getfont(font, size, index, encoding)
IOError: cannot open resource

然后我记得有时候字体还有其他“名字”。或者“文件名”,所以,我做的是去字体文件夹,然后打开Arial字体,显示所有样式,如negrita(粗体),cursiva(斜体)等。

右键点击“negrita”风格,选择的“属性”然后有“真实姓名”字体。

就我而言,名称是“ariblk”

然后,最后,只使用了这样的名字。

font = ImageFont.truetype("C:\Windows\Fonts\\ariblk.ttf",25)

我知道这篇文章很老,但今天帮助我找到解决方案。所以我希望能帮助任何人。

=)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top