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

Python 3.10生成词云遇Pillow textsize弃用警告,求解决方法

问题

在Python 3.10环境中生成词云时,笔记本反复弹出大量如下弃用警告:

DeprecationWarning: textsize is deprecated and will be removed in Pillow 10 (2023-07-01). Use textbbox or textlength instead.

相同代码在Python 3.7环境运行时无此警告,所用代码如下:

def wordcloud(self) -> None:
    """Function to generate wordcloud"""
    long_string = ','.join(self.corpus)
    # Create a WordCloud object
    wordcloud = WordCloud(background_color="white",
                          max_words=5000,
                          contour_width=3,
                          contour_color='steelblue')
    # Generate a word cloud
    wordcloud.generate(long_string)
    # Visualize the word cloud
    plt.figure(figsize=(10, 5))
    plt.imshow(wordcloud, interpolation="bilinear")
    plt.axis('off')
    plt.show()

解决方法

  • 升级wordcloud库:警告根源是旧版wordcloud调用了Pillow高版本(≥9.1.0)中已弃用的textsize方法,升级到最新版wordcloud即可解决兼容问题,执行命令:

    pip install --upgrade wordcloud
    
  • 临时屏蔽警告(仅应急使用):如果暂时无法升级库,可在代码开头添加以下代码屏蔽该特定警告:

    import warnings
    warnings.filterwarnings("ignore", category=DeprecationWarning, message="textsize is deprecated and will be removed in Pillow 10")
    

    注意:此方法仅隐藏警告,未解决根本问题,当Pillow升级到10.x版本后,旧版wordcloud可能直接报错,因此优先推荐升级wordcloud。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 23:10:55