使用Pillow无法调用Futura粗体字体?求解决方案
解决Pillow调用Futura字体粗体变体报错问题
问题场景
在macOS Monterey系统上,使用Pillow 9.4.0 + Python 3.11开发时,尝试为图片添加Futura字体的粗体文本,Font Book和Preview显示该字体存在粗体变体,但执行以下代码触发OSError,提示该字体不是可变字体:
title_font = ImageFont.truetype("Supplemental/Futura.ttc") title_font.set_variation_by_name("bold")
报错堆栈
../helper/ThumbnailCreator.py:38: in add_text title_font.set_variation_by_name("bold") ../../venv311/lib/python3.11/site-packages/PIL/ImageFont.py:834: in set_variation_by_name names = self.get_variation_names() _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = <PIL.ImageFont.FreeTypeFont object at 0x106c08dd0> def get_variation_names(self): """ :returns: A list of the named styles in a variation font. :exception OSError: If the font is not a variation font. """ try: > names = self.font.getvarnames() E OSError: invalid argument ../../venv311/lib/python3.11/site-packages/PIL/ImageFont.py:823: OSError
Pillow环境检测信息
python -m PIL -------------------------------------------------------------------- Pillow 9.4.0 Python 3.11.0rc1 (v3.11.0rc1:41cb07120b, Aug 5 2022, 11:44:46) [Clang 13.0.0 (clang-1300.0.29.30)] -------------------------------------------------------------------- Python modules loaded from /Users/jb/PycharmProjects/blog_content_creatronix/venv311/lib/python3.11/site-packages/PIL Binary modules loaded from /Users/jb/PycharmProjects/blog_content_creatronix/venv311/lib/python3.11/site-packages/PIL -------------------------------------------------------------------- --- PIL CORE support ok, compiled for 9.4.0 --- TKINTER support ok, loaded 8.6 --- FREETYPE2 support ok, loaded 2.12.1 --- LITTLECMS2 support ok, loaded 2.14 --- WEBP support ok, loaded 1.3.0 --- WEBP Transparency support ok --- WEBPMUX support ok --- WEBP Animation support ok --- JPEG support ok, compiled for libjpeg-turbo 2.1.4 --- OPENJPEG (JPEG2000) support ok, loaded 2.5.0 --- ZLIB (PNG/ZIP) support ok, loaded 1.2.13 --- LIBTIFF support ok, loaded 4.5.0 *** RAQM (Bidirectional Text) support not installed *** LIBIMAGEQUANT (Quantization method) support not installed --- XCB (X protocol) support ok -------------------------------------------------------------------- BLP Extensions: .blp Features: open, save, encode -------------------------------------------------------------------- BMP image/bmp Extensions: .bmp Features: open, save -------------------------------------------------------------------- BUFR Extensions: .bufr Features: open, save -------------------------------------------------------------------- CUR Extensions: .cur Features: open -------------------------------------------------------------------- DCX Extensions: .dcx Features: open -------------------------------------------------------------------- DDS Extensions: .dds Features: open, save -------------------------------------------------------------------- DIB image/bmp Extensions: .dib Features: open, save -------------------------------------------------------------------- EPS application/postscript Extensions: .eps, .ps Features: open, save -------------------------------------------------------------------- FITS Extensions: .fit, .fits Features: open, save -------------------------------------------------------------------- FLI Extensions: .flc, .fli Features: open -------------------------------------------------------------------- FTEX Extensions: .ftc, .ftu Features: open -------------------------------------------------------------------- GBR Extensions: .gbr Features: open -------------------------------------------------------------------- GIF image/gif Extensions: .gif Features: open, save, save_all -------------------------------------------------------------------- GRIB Extensions: .grib Features: open, save -------------------------------------------------------------------- HDF5 Extensions: .h5, .hdf Features: open, save -------------------------------------------------------------------- ICNS image/icns Extensions: .icns Features: open, save -------------------------------------------------------------------- ICO image/x-icon Extensions: .ico Features: open, save -------------------------------------------------------------------- IM Extensions: .im Features: open, save -------------------------------------------------------------------- IMT Features: open -------------------------------------------------------------------- IPTC Extensions: .iim Features: open -------------------------------------------------------------------- JPEG image/jpeg Extensions: .jfif, .jpe, .jpeg, .jpg Features: open, save -------------------------------------------------------------------- JPEG2000 image/jp2 Extensions: .j2c, .j2k, .jp2, .jpc, .jpf, .jpx Features: open, save -------------------------------------------------------------------- MCIDAS Features: open -------------------------------------------------------------------- MPEG video/mpeg Extensions: .mpeg, .mpg Features: open -------------------------------------------------------------------- MSP Extensions: .msp Features: open, save, decode -------------------------------------------------------------------- PCD Extensions: .pcd Features: open -------------------------------------------------------------------- PCX image/x-pcx Extensions: .pcx Features: open, save -------------------------------------------------------------------- PIXAR Extensions: .pxr Features: open -------------------------------------------------------------------- PNG image/png Extensions: .apng, .png Features: open, save, save_all -------------------------------------------------------------------- PPM image/x-portable-anymap Extensions: .pbm, .pgm, .pnm, .ppm Features: open, save -------------------------------------------------------------------- PSD image/vnd.adobe.photoshop Extensions: .psd Features: open -------------------------------------------------------------------- SGI image/sgi Extensions: .bw, .rgb, .rgba, .sgi Features: open, save -------------------------------------------------------------------- SPIDER Features: open, save -------------------------------------------------------------------- SUN Extensions: .ras Features: open -------------------------------------------------------------------- TGA image/x-tga Extensions: .icb, .tga, .vda, .vst Features: open, save -------------------------------------------------------------------- TIFF image/tiff Extensions: .tif, .tiff Features: open, save, save_all -------------------------------------------------------------------- WEBP image/webp Extensions: .webp Features: open, save, save_all -------------------------------------------------------------------- WMF Extensions: .emf, .wmf Features: open, save -------------------------------------------------------------------- XBM image/xbm Extensions: .xbm Features: open, save -------------------------------------------------------------------- XPM image/xpm Extensions: .xpm Features: open -------------------------------------------------------------------- XVTHUMB Features: open --------------------------------------------------------------------
解决方法
原因说明
Futura.ttc是TrueType字体集合(TTC),而非可变字体。可变字体支持通过set_variation_by_name动态调整样式,但TTC是将多个独立字体打包在一个文件中,每个样式(如粗体)都是独立的字体实例,无法用可变字体的方式调用。
方案1:通过索引指定粗体字体
TTC文件中的每个字体都有对应的索引,直接指定索引加载粗体变体:
# 尝试index=1或其他数值,根据TTC内的字体顺序调整 title_font = ImageFont.truetype("Supplemental/Futura.ttc", index=1)
可通过fc-list命令(需先通过brew安装fontconfig:brew install fontconfig)查看TTC内的字体信息:
fc-list /Library/Fonts/Supplemental/Futura.ttc
方案2:直接调用粗体字体名称
如果知道Futura粗体的具体字体名,直接指定名称加载:
title_font = ImageFont.truetype("Futura Bold")
方案3:使用绝对路径加载
直接指定字体文件的绝对路径,避免路径解析问题:
title_font = ImageFont.truetype("/Library/Fonts/Supplemental/Futura.ttc", index=1)
内容的提问来源于stack exchange,提问作者Joern Boegeholz
相关产品推荐
相关产品推荐

