看来,在我的计算机上,我只有一个文件用于Ubuntu压缩字体。即:
/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-C.ttf
但是,在LibreOffice中,我可以将该字体设为粗体和斜体:
在Ubuntu font tester网页上,我还可以为Ubuntu Condensed系列设置不同的字体粗细(即常规和粗体)和不同的字体样式(即常规和斜体)。在CSS中,这可能会转换为:
/* CSS for bold and italic Ubuntu Condensed font */
font-family: Ubuntu Condensed;
font-style: italic;
font-weight: 700;
或:
/* CSS for regular and normal Ubuntu Condensed font */
font-family: Ubuntu Condensed;
font-style: normal;
font-weight: 400;
但是,关于Python的PIL / Pillow,除了用ImageFont函数加载不同的字体文件外,我没有在ImageFont.truetype()
中找到任何可以修改字体样式和/或粗细的工作选项。
对于“常规” Ubuntu字体(不压缩),我可以加载四个单独的文件之一,以在Python中获得常规,粗体,斜体和粗体+斜体样式:
/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-R.ttf
/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-RI.ttf
/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-B.ttf
/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-BI.ttf
但不适用于Ubuntu Condensed。
所以问题是,是否可以在PIL / Pillow中以粗体和/或斜体Ubuntu Condensed字体绘制图像?还是至少有可能生成四个Ubuntu Condensed文件,以便可以像Python中的Ubuntu字体一样使用它?
get_variation_names()
,该方法应为您提供名称列表。>>> from PIL import ImageFont
>>>
>>> font = ImageFont.truetype(...)
>>> font.get_variation_names()
[b'Normal', b'Bold', b'Italic', b'Bold, Italic']
>>> font.set_variation_by_name('Italic')
这应该为您提供所需的字体变体。