问题bug:
AttributeError: ‘ImageDraw‘ object has no attribute ‘textsize‘
原因分析
ImageDraw在高版本中移除了textsize函数,高版本中无法直接使用imagedraw.textsize:
textsize is deprecated and will be removed in Pillow 10 (2023-07-01). Use textbbox or textlength instead.
解决方案:
- 版本回退
使用10.0以下的版本,不要使用高版本(不建议采用,如果在项目初期可以使用)
- 使用textbox属性
修改代码如下:
#左上和右下坐标
textbox = (50, 50, 350, 150)
# 在指定的文本框中绘制文本,并返回文本的实际范围
text_bounds = draw.textbox(textbox, text, font=font)
# 计算文本的实际宽度和高度
text_width = text_bounds[2] - text_bounds[0]
text_height = text_bounds[3] - text_bounds[1]
3568

被折叠的 条评论
为什么被折叠?



