Python,PIL;文本到图像和字体

2024-04-28 20:43:08 发布

您现在位置:Python中文网/ 问答频道 /正文

在Python和PIL下向图像写入文本有问题- 我可以将文本写入png文件,但不能使用粗体文本。有谁能举个例子说明如何做到这一点吗?

我想最简单的解决办法可能是使用文本的粗体变体,但是我在Windows/font文件夹中看不到任何提供此功能的内容-这是否意味着字体类型具有T/F的“粗体属性”?:
quick look for bold-fonts under windows

我使用的代码:

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

Tags: fromimage文本importimgpilpngttf
2条回答

您不会在“控制面板”中查看实际的字体文件(在“Windows/fonts”文件夹中,资源管理器也会神奇地变成“字体查看器”控制面板),它们是按族分组的,以方便您查看。双击族以查看族中的字体:

enter image description here

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

enter image description here

一个简单的方法:

font = ImageFont.load_default().font

您还可以执行google search for 'verdana.ttf'并将其下载到与python文件相同的目录中:

然后这样添加:

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

相关问题 更多 >