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

时间:2022-11-08 00:25:55

I have an issue with writing text to an image under Python and PIL - I'm able to write text to a png file, though not bold text. Could anyone provide an example of how to achieve this?

我有一个问题,在Python和PIL下将文本写入图像 - 我能够将文本写入png文件,但不是粗体文本。谁能提供一个如何实现这一目标的例子?

I thought the easiest solution may be was use a bold-variant of a text, but I'm unable to see anything in the Windows/font folder that supplies this - does this mean font types have a 'bold attribute' that is T/F?:
Python,PIL;文本到图像和字体

我认为最简单的解决方案可能是使用文本的粗体变体,但我无法在Windows / font文件夹中看到提供此内容的任何内容 - 这是否意味着字体类型具有'粗体属性'即T / F?:

Code I'm using:

我正在使用的代码:

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 个解决方案

#1


8  

You aren't looking at actual font files in the control panel (explorer magically turns into the font viewer control panel when in the Windows/fonts folder as well), they are grouped by family for your convenience. Double click the family to see the fonts in the family:

您没有在控制面板中查看实际的字体文件(在Windows / fonts文件夹中,资源管理器会神奇地变成字体查看器控制面板),为方便起见,它们按家庭分组。双击该族以查看该族中的字体:

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

Then right-click and choose properties to find the file name:

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

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

#2


11  

A simple way to do it:

一个简单的方法:

font = ImageFont.load_default().font

Also you can do a google search for 'verdana.ttf' and download it put it in the same directory as the python file:

你也可以谷歌搜索'verdana.ttf'并下载它把它放在与python文件相同的目录中:

Then add it like this:

然后像这样添加:

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

#1


8  

You aren't looking at actual font files in the control panel (explorer magically turns into the font viewer control panel when in the Windows/fonts folder as well), they are grouped by family for your convenience. Double click the family to see the fonts in the family:

您没有在控制面板中查看实际的字体文件(在Windows / fonts文件夹中,资源管理器会神奇地变成字体查看器控制面板),为方便起见,它们按家庭分组。双击该族以查看该族中的字体:

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

Then right-click and choose properties to find the file name:

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

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

#2


11  

A simple way to do it:

一个简单的方法:

font = ImageFont.load_default().font

Also you can do a google search for 'verdana.ttf' and download it put it in the same directory as the python file:

你也可以谷歌搜索'verdana.ttf'并下载它把它放在与python文件相同的目录中:

Then add it like this:

然后像这样添加:

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