Python,PIL; 文字转图像和字体

22
我在使用Python和PIL时遇到了一个问题,需要将文本写入图像 - 我能够将文本写入png文件,但无法写入粗体文本。有人能提供如何实现这个的示例吗?
我认为最简单的解决方案可能是使用文本的粗体变体,但我无法在Windows / font文件夹中看到任何提供此功能的内容 - 这是否意味着字体类型具有“粗体属性”的真假值?:
我正在使用的代码:
import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw

# font = ImageFont.truetype("Arial-Bold.ttf",14)
font = ImageFont.truetype("Arial.ttf",14)
img=Image.new("RGBA", (500,250),(255,255,255))
draw = ImageDraw.Draw(img)
draw.text((0, 0),"This is a test",(0,0,0),font=font)
draw = ImageDraw.Draw(img)
img.save("a_test.png")
2个回答

16

一个简单的方法:

font = ImageFont.load_default().font

你也可以进行谷歌搜索Verdana.ttf字体并下载,将其放在与Python文件相同的目录中:

然后像这样添加:

font = ImageFont.truetype("Verdana.ttf",14)

13

您在控制面板中并不是查看实际的字体文件(当进入Windows/fonts文件夹时,资源管理器会神奇地转换为字体查看器控制面板),它们按系列进行分组以方便您使用。双击系列以查看系列中的字体:

图片描述

然后右键单击并选择属性以查找文件名:

图片描述


1
谢谢 Pavel,这很好用:font = ImageFont.truetype("Arialbd.ttf",14) - Harry Lime

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接