大熊的饲养员 2023-05-09 17:20 采纳率: 50%
浏览 263

词云分析时,出现的错误

大数据分析,使用python生成词云图时,发生一下错误:

import jieba
from wordcloud import WordCloud
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
with open('Dream_of_the_Red_Mansion.txt', 'r', encoding='utf-8') as f:
    text = f.read()
# 对文本进行分词
words = jieba.cut(text)
result = " ".join(words)
# 生成词云图
image_background = Image.open('Red.jpg')
MASK = np.array(image_background)
wordcloud = WordCloud(font_path='SimHei.ttf',background_color='white', width=4000, 
                      height=2000, margin=10, max_words=200, mask=MASK).generate(result)
plt.imshow(wordcloud)
plt.show()
wordcloud.to_file('final.png')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-41-eaad86f251cc> in <module>
     16 MASK = np.array(image_background)
     17 
---> 18 wordcloud = WordCloud(font_path='SimHei.ttf',background_color='white', width=4000, 
     19                       height=2000, margin=10, max_words=200, mask=MASK).generate(result)
     20 

~/.local/lib/python3.8/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):

~/.local/lib/python3.8/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 

~/.local/lib/python3.8/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

~/.local/lib/python3.8/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,

AttributeError: 'ImageDraw' object has no attribute 'textbbox'

请问该如何解决?

  • 写回答

2条回答 默认 最新

  • Leodong. 2023-05-09 17:21
    关注

    该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:
    这个错误通常是由于Pillow版本不兼容导致的。

    您可以尝试更新Pillow版本到最新版本来解决此问题。可以使用以下命令来更新Pillow:

    pip install -U pillow
    

    如果更新Pillow后仍然出现问题,您可以尝试使用旧版本的WordCloud库。可以使用以下命令来安装旧版本的WordCloud:

    pip install wordcloud==1.8.1
    

    如果仍然无法解决问题,您可以尝试使用不同的字体文件。可以在WordCloud初始化时使用其他中文字体文件,例如:

    wordcloud = WordCloud(font_path='STKAITI.TTF', background_color='white', width=4000, height=2000, margin=10, max_words=200, mask=MASK).generate(result)
    

    希望这些解决方案能够帮助您解决问题。


    如果以上回答对您有所帮助,点击一下采纳该答案~谢谢

    评论

报告相同问题?

问题事件

  • 创建了问题 5月9日

悬赏问题

  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表