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

WordCloud生成报错:'TransposedFont'无'getbbox'属性的解决咨询

词云生成报错:AttributeError: 'TransposedFont' object has no attribute 'getbbox'

问题描述

我运行以下代码生成词云:

text = 'word1 word2 word2 word3 word3 word3'

from wordcloud import WordCloud

wordcloud = WordCloud(width=480, height=480).generate(text)

在Jupyter Notebook中得到完整错误栈:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-17-659d2fbc8555> in <module>
     8 
     9 # Generate the word cloud
---> 10 wordcloud.generate(text)
    11 
    12 # Display the word cloud

~\anaconda3\lib\site-packages\wordcloud\wordcloud.py in generate(self, text)
   637         self
   638         """
---> 639         return self.generate_from_text(text)
   640 
   641     def _check_generated(self):

~\anaconda3\lib\site-packages\wordcloud\wordcloud.py in generate_from_text(self, text)
   619         """
   620         words = self.process_text(text)
---> 621         self.generate_from_frequencies(words)
   622         return self
   623 

~\anaconda3\lib\site-packages\wordcloud\wordcloud.py in generate_from_frequencies(self, frequencies, max_font_size)
   451                 font_size = self.height
   452             else:
---> 453                 self.generate_from_frequencies(dict(frequencies[:2]),
   454                                                max_font_size=self.height)
   455                 # find font sizes

~\anaconda3\lib\site-packages\wordcloud\wordcloud.py in generate_from_frequencies(self, frequencies, max_font_size)
   506                     font, orientation=orientation)
   507                 # get size of resulting text
---> 508                 box_size = draw.textbbox((0, 0), word, font=transposed_font, anchor="lt")
   509                 # find possible places using integral image:
   510                 result = occupancy.sample_position(box_size[3] + self.margin,

~\anaconda3\lib\site-packages\PIL\ImageDraw.py in textbbox(self, xy, text, font, anchor, spacing, align, direction, features, language, stroke_width, embedded_color)
   565             font = self.getfont()
   566         mode = "RGBA" if embedded_color else self.fontmode
---> 567         bbox = font.getbbox(
   568             text, mode, direction, features, language, stroke_width, anchor
   569         )

AttributeError: 'TransposedFont' object has no attribute 'getbbox'

我未在WordCloud函数中使用过TransposedFont相关参数,请问该错误是什么原因,如何解决?

错误原因与解决方法

错误原因

这个报错是wordcloud库与PIL(Pillow)库版本不兼容导致的:

  • 新版Pillow调整了字体相关API,TransposedFont对象不再提供getbbox方法;
  • 旧版本wordcloud仍在调用这个已被移除的方法,从而触发AttributeError。

解决方法

有两种可行的修复方式:

  • 方法1:升级wordcloud到兼容版本
    执行以下命令更新wordcloud,新版本已适配Pillow的API变更:
    pip install --upgrade wordcloud
    
  • 方法2:降级Pillow到兼容旧版
    如果暂时不想升级wordcloud,可将Pillow降级到9.5.0版本(该版本与旧版wordcloud兼容):
    pip install pillow==9.5.0
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 13:25:10