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

发布于 2024-08-12 08:17:09 字数 69 浏览 8 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

少跟Wǒ拽 2024-08-19 08:17:09

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

Use the bold/italic version of the font

柠檬色的秋千 2024-08-19 08:17:09

到目前为止,还没有粗体功能作为参数,但您可以通过向具有相同颜色的文本的文本添加笔划来轻松解决。下一个代码详细说明如何使用笔画,这将是有意义的粗体字体

    draw.text((x, y), text, fill=color, font=font, stroke_width=2,
          stroke_fill="black")

There is no bold features as parameter till now but easily you can solve by add stroke to the text with same color of text. it will make sense as bold font next code elaborate how to use stroke

    draw.text((x, y), text, fill=color, font=font, stroke_width=2,
          stroke_fill="black")
流心雨 2024-08-19 08:17:09

如果(无论出于何种原因)您没有单独的粗体版本的字体,则将字体设置为粗体的一个相当老套的解决方案是多次打印相同的文本并稍微偏移。

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

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')
撑一把青伞 2024-08-19 08:17:09

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

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

浅紫色的梦幻 2024-08-19 08:17:09

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

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

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)

我知道这篇文章已经过时了,但今天帮助我找到了解决方案,所以我希望对任何人都有帮助

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.

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

In my case, the name was "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.

=)

月寒剑心 2024-08-19 08:17:09

参考此处的其他答案,我对 Arial 粗体变体的名称的搜索产生了以下内容(arialbd.ttf):

def FindFontsVariantsWithBase(fontBase="arial"):
        import matplotlib
        system_fonts = matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')
        # for font in np.sort(system_fonts):
        #     print(font)
        fonts = np.sort(system_fonts).tolist()
        res = [i for i in fonts if fontBase in i]
        print(res)
        
FindFontsVariantsWithBase("arial")

['C:\WINDOWS\Fonts\arial.ttf', 'C:\WINDOWS\Fonts\ arialbd.ttf'、'C:\WINDOWS\Fonts\arialbi.ttf'、'C:\WINDOWS\Fonts\ariali.ttf'、'C:\Windows\Fonts\arial.ttf'、'C:\Windows\字体\arialbd.ttf', 'C:\Windows\Fonts\arialbi.ttf', 'C:\Windows\Fonts\ariali.ttf'] (

With reference to the other answers here, my search for the name for the bold variant of Arial produced the following (arialbd.ttf):

def FindFontsVariantsWithBase(fontBase="arial"):
        import matplotlib
        system_fonts = matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')
        # for font in np.sort(system_fonts):
        #     print(font)
        fonts = np.sort(system_fonts).tolist()
        res = [i for i in fonts if fontBase in i]
        print(res)
        
FindFontsVariantsWithBase("arial")

['C:\WINDOWS\Fonts\arial.ttf', 'C:\WINDOWS\Fonts\arialbd.ttf', 'C:\WINDOWS\Fonts\arialbi.ttf', 'C:\WINDOWS\Fonts\ariali.ttf', 'C:\Windows\Fonts\arial.ttf', 'C:\Windows\Fonts\arialbd.ttf', 'C:\Windows\Fonts\arialbi.ttf', 'C:\Windows\Fonts\ariali.ttf'] (

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文