You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pillow中ImageFont.truetype报OSError: invalid argument问题求助

解决Pillow 9.5.0调用ImageFont.truetype时的OSError: invalid argument错误
  • 错误原因:调用ImageFont.truetype时,第三个位置参数传了整数10,该参数对应index(字体文件内的字体索引),而arial.ttf通常只包含1个字体,有效索引为0,传10属于无效索引,触发了错误。

  • 修正后的代码:

draw.font = ImageFont.truetype(
    'C:\\Windows\\Fonts\\arial.ttf',
    32,
    layout_engine=ImageFont.LAYOUT_BASIC,
)

如果确实需要指定字体索引(比如多字体文件),请明确传入有效索引值(通常为0):

draw.font = ImageFont.truetype(
    'C:\\Windows\\Fonts\\arial.ttf',
    32,
    index=0,
    layout_engine=ImageFont.LAYOUT_BASIC,
)

补充说明:Pillow 9.5.0中ImageFont.truetype的参数顺序为font, size, index=0, encoding='', layout_engine=None,位置传参时需严格对应,不确定参数位置时建议使用关键字传参避免错误。

内容的提问来源于stack exchange,提问作者Jcheong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 17:45:00