简体   繁体   English

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

[英]Python, PIL; Text to Image and fonts

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. 我有一个问题,在Python和PIL下将文本写入图像 - 我能够将文本写入png文件,但不是粗体文本。 Could anyone provide an example of how to achieve this? 谁能提供一个如何实现这个目标的例子?

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

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

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)

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. 您不是在控制面板中查看实际的字体文件(在Windows / fonts文件夹中,资源管理器会神奇地变成字体查看器控制面板),为方便起见,它们按系列分组。 Double click the family to see the fonts in the family: 双击该族以查看该族中的字体:

在此输入图像描述

Then right-click and choose properties to find the file name: 然后右键单击并选择属性以查找文件名:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM