wordcloud: raise ValueError(“Only supported for TrueType fonts“)

在尝试使用matplotlib和wordcloud库绘制词云图时,遇到了ValueError,提示只支持TrueType字体。错误源于Pillow包的问题。作者首先尝试升级Pillow,但因缺少zlib库而失败。最终,作者通过指定Pillow的一个特定版本(9.2.0)成功避免了错误,完成了词云图的绘制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先,俺的需求是‘要用matplotlib 和wordcloud 绘制词云图

import matplotlib.pyplot as plt
from wordcloud import WordCloud
import jieba
text='哇哈技术附件啊离开家'
text=' '.join(jieba.lcut(text))
wordcloud = WordCloud(font_path="D:\\编程\python\\support\\font\\simfang_downyi.com\\simfang_ttf\\simfang.ttf").generate(text)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()

结果报错:

Building prefix dict from the default dictionary ...     
Loading model from cache 
C:\Users\35470\AppData\Local\Temp\jieba.cache                                                  
Loading model cost 0.533 seconds.                                                                                 
Prefix dict has been built successfully.                                                                             

Traceback (most recent call last):  

.......(省略若干无效信息)......

raise ValueError("Only supported for TrueType fonts")   
                                                           
ValueError: Only supported for TrueType fonts

在优快云上搜索若干,都说是Pillow包没更新的原因,github上也是类似:

 于是开始想方设法升级Pillow,不过,报错总是出现:“ ERROR: Failed building wheel for Pillow                                                              

Failed to build Pillow                                                                                                

 ERROR: Could not build wheels for Pillow, which is required to install pyproject.toml-based projects ”

The headers or library files could not be found for zlib,
a required dependency when compiling Pillow from source.
Please see the install instructions at:
https://pillow.readthedocs.io/en/latest/installation.html  

.......(省略若干无效信息)......

ERROR: Failed building wheel for Pillow 
Failed to build Pillow
ERROR: Could not build wheels for Pillow, which is required to install pyproject.toml-based projects 

最后真是整崩溃了,突然想起来或许可以换其他版本(默认更新的是最新版本),使用以下code查看可以使用的版本:

pip3 install --upgrade Pillow== 

俺试了个:

pip3 install --upgrade Pillow==9.2.0

我天!终于不再报bug!:

 感谢收看🥰🥰🥰

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值