WordCloud仅支持TrueType字体。

8

我正在尝试使用Python中的WordCloud模块生成词云,但是每当我调用.generate时,我会看到以下错误。

Traceback (most recent call last):
  File "/mnt/6db3226b-5f96-4257-980d-bb8ec1dad8e7/test.py", line 4, in <module>
    wc.generate("foo bar foo bar hello world")
  File "/home/mjc/.local/lib/python3.10/site-packages/wordcloud/wordcloud.py", line 639, in generate
    return self.generate_from_text(text)
  File "/home/mjc/.local/lib/python3.10/site-packages/wordcloud/wordcloud.py", line 621, in generate_from_text
    self.generate_from_frequencies(words)
  File "/home/mjc/.local/lib/python3.10/site-packages/wordcloud/wordcloud.py", line 453, in generate_from_frequencies
    self.generate_from_frequencies(dict(frequencies[:2]),
  File "/home/mjc/.local/lib/python3.10/site-packages/wordcloud/wordcloud.py", line 508, in generate_from_frequencies
    box_size = draw.textbbox((0, 0), word, font=transposed_font, anchor="lt")
  File "/usr/lib/python3/dist-packages/PIL/ImageDraw.py", line 671, in textbbox
    raise ValueError("Only supported for TrueType fonts")
ValueError: Only supported for TrueType fonts

目前,我正在尝试创建一个非常简单的词云示例

import matplotlib.pyplot as plt
from wordcloud import WordCloud
wc = WordCloud(background_color="white", font_path="./arial.ttf", width=800, height=400)
wc.generate("foo bar foo bar hello world")
plt.axis("off")
plt.imshow(wc)
plt.savefig("test.png")
plt.show()

在Ubuntu 22.04和Python 3.10.6下,我将arial.ttfhttps://www.freefontspro.com/14454/arial.ttf下载并放置在与test.py相同的目录中。

我希望能够从输入“foo bar foo bar hello world”生成词云,但是出现了错误ValueError:Only supported for TrueType fonts,尽管已将ttf传递给font_path参数。

如果有任何帮助解决此问题,将不胜感激!

4个回答

9

以下方法适用于我:

pip install --upgrade pip

pip install --upgrade Pillow

如果不行,请告诉我。


我尝试安装/升级Pillow,但仍然遇到相同的问题。 - Matthew
1
这对我有用,尽管我忽略了你的答案并首先尝试了被接受的答案。谢谢!顺便说一下,不确定是否相关,但在我的情况下(Windows),pillow包名称是小写的。 - abu
似乎pillow是Windows的解决方案,而Ubuntu则需要libfreetype6。我将保留Mark的答案作为已接受的答案,因为它对我的情况有所帮助。 - Matthew
在更新了词云库之后,我遇到了同样的问题。不幸的是,在我的环境中(从WSL shell启动的jupyter笔记本),所有的解决方法都没有帮助。libfreetype6已经是最新版本,并且我将pillow更新到了9.5.0版本。然而,我仍然收到相同的错误信息。 - eel76
1
这对我有效 - code by Abhishek Bharti
这对我也有效。非常感谢! - Krasnars

2

即使我下载了相同的字体,也无法在Python 3.10上重现您的错误 - 尽管我使用的是macOS。

我能想象的唯一可能是你的PIL不支持TrueType字体,因此您可以检查一下:

python3 -m PIL

样例输出

--------------------------------------------------------------------
Pillow 9.4.0
Python 3.10.0 (v3.10.0:b494f5935c, Oct  4 2021, 14:59:19) [Clang 12.0.5 (clang-1205.0.22.11)]
--------------------------------------------------------------------
Python modules loaded from /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/PIL
Binary modules loaded from /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 9.4.0
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.13.0             <--- HERE
--- LITTLECMS2 support ok, loaded 2.14
--- WEBP support ok, loaded 1.3.0
--- WEBP Transparency support ok
--- WEBPMUX support ok
--- WEBP Animation support ok
--- JPEG support ok, compiled for libjpeg-turbo 2.1.4
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.0
--- ZLIB (PNG/ZIP) support ok, loaded 1.2.13
--- LIBTIFF support ok, loaded 4.5.0
--- RAQM (Bidirectional Text) support ok, loaded 0.9.0, fribidi 1.0.12, harfbuzz 7.1.0
*** LIBIMAGEQUANT (Quantization method) support not installed
--- XCB (X protocol) support ok
--------------------------------------------------------------------
BLP
Extensions: .blp
Features: open, save, encode
--------------------------------------------------------------------
BMP image/bmp
Extensions: .bmp
Features: open, save
--------------------------------------------------------------------
BUFR
Extensions: .bufr
Features: open, save
--------------------------------------------------------------------
CUR
Extensions: .cur
Features: open
--------------------------------------------------------------------
DCX
Extensions: .dcx
Features: open
--------------------------------------------------------------------
DDS
Extensions: .dds
Features: open, save
--------------------------------------------------------------------
DIB image/bmp
Extensions: .dib
Features: open, save
--------------------------------------------------------------------
EPS application/postscript
Extensions: .eps, .ps
Features: open, save
--------------------------------------------------------------------
FITS
Extensions: .fit, .fits
Features: open, save
--------------------------------------------------------------------
FLI
Extensions: .flc, .fli
Features: open
--------------------------------------------------------------------
FTEX
Extensions: .ftc, .ftu
Features: open
--------------------------------------------------------------------
GBR
Extensions: .gbr
Features: open
--------------------------------------------------------------------
GIF image/gif
Extensions: .gif
Features: open, save, save_all
--------------------------------------------------------------------
GRIB
Extensions: .grib
Features: open, save
--------------------------------------------------------------------
HDF5
Extensions: .h5, .hdf
Features: open, save
--------------------------------------------------------------------
ICNS image/icns
Extensions: .icns
Features: open, save
--------------------------------------------------------------------
ICO image/x-icon
Extensions: .ico
Features: open, save
--------------------------------------------------------------------
IM
Extensions: .im
Features: open, save
--------------------------------------------------------------------
IMT
Features: open
--------------------------------------------------------------------
IPTC
Extensions: .iim
Features: open
--------------------------------------------------------------------
JPEG image/jpeg
Extensions: .jfif, .jpe, .jpeg, .jpg
Features: open, save
--------------------------------------------------------------------
JPEG2000 image/jp2
Extensions: .j2c, .j2k, .jp2, .jpc, .jpf, .jpx
Features: open, save
--------------------------------------------------------------------
MCIDAS
Features: open
--------------------------------------------------------------------
MPEG video/mpeg
Extensions: .mpeg, .mpg
Features: open
--------------------------------------------------------------------
MSP
Extensions: .msp
Features: open, save, decode
--------------------------------------------------------------------
PCD
Extensions: .pcd
Features: open
--------------------------------------------------------------------
PCX image/x-pcx
Extensions: .pcx
Features: open, save
--------------------------------------------------------------------
PIXAR
Extensions: .pxr
Features: open
--------------------------------------------------------------------
PNG image/png
Extensions: .apng, .png
Features: open, save, save_all
--------------------------------------------------------------------
PPM image/x-portable-anymap
Extensions: .pbm, .pgm, .pnm, .ppm
Features: open, save
--------------------------------------------------------------------
PSD image/vnd.adobe.photoshop
Extensions: .psd
Features: open
--------------------------------------------------------------------
SGI image/sgi
Extensions: .bw, .rgb, .rgba, .sgi
Features: open, save
--------------------------------------------------------------------
SPIDER
Features: open, save
--------------------------------------------------------------------
SUN
Extensions: .ras
Features: open
--------------------------------------------------------------------
TGA image/x-tga
Extensions: .icb, .tga, .vda, .vst
Features: open, save
--------------------------------------------------------------------
TIFF image/tiff
Extensions: .tif, .tiff
Features: open, save, save_all
--------------------------------------------------------------------
WEBP image/webp
Extensions: .webp
Features: open, save, save_all
--------------------------------------------------------------------
WMF
Extensions: .emf, .wmf
Features: open, save
--------------------------------------------------------------------
XBM image/xbm
Extensions: .xbm
Features: open, save
--------------------------------------------------------------------
XPM image/xpm
Extensions: .xpm
Features: open
--------------------------------------------------------------------
XVTHUMB
Features: open
--------------------------------------------------------------------

或者更快地说 - 因为我们只对"freetype"行感兴趣:
python3 -m PIL | grep -i type

然后您可能需要使用类似以下命令安装freetype - 不太确定确切的软件包名称:

sudo apt install libfreetype6

太好了!我的 libfreetype6 已经过时,需要手动更新! - Matthew
1
我在几台Windows机器上(Win7,Python 3.8.7,Pillow 9.1.1和Win10,Python 3.9.9,Pillow 9.0.1)遇到了相同的错误消息,都是使用python3 -m PIL命令输出:--- FREETYPE2 support ok。解决方法是将Pillow升级到9.5.0版本:pip install --upgrade pillow,正如@kbogdan在另一个答案中建议的那样。 - abu

0
使用Python 3.6,重新安装了wordcloud 1.8.0(pip install wordcloud==1.8.0),问题已解决。根据官方文档,最新版本的WordCloud不支持Python3.6。

0
尝试了这个解决方案1
升级PIP和Pillow对我来说没有起作用,就像其他很多人一样
尝试了这个解决方案2 - 对我来说效果非常好
使用Python 3.6,重新安装了wordcloud 1.8.0(pip install wordcloud==1.8.0),问题解决了。根据官方文档,最新版本的WordCloud不支持Python3.6。

这应该是对现有答案的评论。除非你有一个之前没有提出的新答案要提供,否则不要使用“答案”。尤其不要证明这些给出的答案对你是否有效。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接