python 用 PIL 绘制粗体/斜体文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1815165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:07:04  来源:igfitidea点击:

Draw bold/italic text with PIL?

pythontextfontspython-imaging-librarydraw

提问by Hyman

How to draw bold/italic text with PIL? ImageFont.truetype(file, size) has an option to specify font size only.

如何使用 PIL 绘制粗体/斜体文本?ImageFont.truetype(file, size) 有一个仅指定字体大小的选项。

采纳答案by John La Rooy

Use the bold/italic version of the font

使用粗体/斜体版本的字体

回答by djc

Many fonts use different TTF files for their bold/italic versions, so I'd imagine if you just specify that file it would work.

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

回答by zara

A rather hacky solution to make a font bold if (for whatever reason) you don't have a separate bold version of the font is to print the same text several times with a slight offset.

如果(无论出于何种原因)您没有单独的粗体版本的字体,则使字体变粗的一个相当笨拙的解决方案是多次打印相同的文本并带有轻微偏移。

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')

回答by Николаи Милош

Well, this is my first comment. Here we go.

嗯,这是我的第一条评论。开始了。

I'll try to clarify the procedure. At first What I did was use the "name" of the font like this

我会尽力澄清程序。起初我所做的是像这样使用字体的“名称”

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

but only got some errors like this:

但只有这样的一些错误:

    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

Then I remembered that sometimes fonts has other "names" or "filenames", so, what I did was going to fonts folder, then opened the Arial Font wich displayed all the styles like negrita (bold], cursiva(italic), etc.

然后我想起有时字体有其他“名称”或“文件名”,所以,我所做的是转到字体文件夹,然后打开 Arial 字体,显示所有样式,如 negrita(粗体)、cursiva(斜体)等。

Did a right click on the "negrita" style, selected "properties" and then there was the "real name" of the font.

右键单击“negrita”样式,选择“属性”,然后是字体的“真实姓名”。

In my case, the name was "ariblk"

就我而言,名字是“ariblk”

Then, finally, just used the name like this.

然后,最后,就这样使用了这个名字。

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

I know this post is old, but today helped me to get to the solution. So I hope to help anybody.

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

=)

=)