使用 ‘textsize’ 的目标通常是拿到图像中文字的尺寸,结果发现 CSDN 和 知乎上有很多类似的代码,均使用了 ImageDraw.textsize()
而通过查阅 PIL 库的官方文档,发现 textsize() 函数被 remove 掉了,现在用的话可以使用ImageDraw.textbbox(), ImageDraw.textlength() and ImageDraw.multiline_textbbox()
(文档指路https://pillow.readthedocs.io/en/stable/releasenotes/10.0.0.html#font-size-and-offset-methods)
以 ImageDraw.textbbox() 为例
该函数总结如下,更详细的信息可参考文档https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html#PIL.ImageDraw.ImageDraw.textbbox
ImageDraw.textbbox(xy, text, font=None, anchor=None, spacing=4, align='left', direction=None, features=None, language=None, stroke_width=0, embedded_color=False, font_size=None)[source]
返回给定文本相对于给定锚点在指定字体、方向、特性和语言下渲染时的边界框(以像素为单位)。仅支持