AttributeError: ‘FreeTypeFont‘ object has no attribute ‘getsize‘

10 阅读1分钟

说明:在一次程序读取字体样式,想要获取字体尺寸时,报下面的错误;

AttributeError: 'FreeTypeFont' object has no attribute 'getsize'

问题

    # 加载字体样式和设置字体大小
    font = ImageFont.truetype("SourceCodePro-Bold.ttf", size=1)
    # 得出字符的纵横比
    aspect_ratio = font.getsize("x")[0] / font.getsize("x")[1]

说该对象没有getSize方法

在这里插入图片描述

解决

参考下面这篇文章,将getSize方法换成getbbox方法;

getboox("x")方法的返回值,后面两个即为该字体的宽、高;

在这里插入图片描述

首次发布

hezhongying.blog.csdn.net/article/det…