我正在try 使用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

现在,我正在try 创建一个非常简单的示例wordCloud

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()

其中arial.ttf是从https://www.freefontspro.com/14454/arial.ttf下载的,并且放置在与test.py相同的目录中.我使用的是Ubuntu 22.04和Python3.10.6.

我原本希望从输入"foo bar foo bar Hello world"生成一个单词云,但是看到错误ValueError: Only supported for TrueType fonts,尽管向font_path参数传递了一个TTF.

任何帮助解决这一问题的人都将不胜感激!

推荐答案

即使在下载了相同的字体后,我也无法用Python3.10重现您的错误--尽管我使用的是MacOS.

我唯一能想象的是你的PIL不支持TrueType字体,所以你可以判断一下:

python3 -m PIL

Sample Output

--------------------------------------------------------------------
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

Python相关问答推荐

从管道将Python应用程序部署到Azure Web应用程序,不包括需求包

DuckDB将蜂巢分区插入拼花文件

如何使用Python中的clinicalTrials.gov API获取完整结果?

从包含数字和单词的文件中读取和获取数据集

在Python中为变量的缺失值创建虚拟值

提取两行之间的标题的常规表达

acme错误-Veritas错误:模块收件箱没有属性linear_util'

将特定列信息移动到当前行下的新行

仿制药的类型铸造

当使用keras.utils.Image_dataset_from_directory仅加载测试数据集时,结果不同

' osmnx.shortest_track '返回有效源 node 和目标 node 的'无'

大小为M的第N位_计数(或人口计数)的公式

如何在python polars中停止otherate(),当使用when()表达式时?

我们可以为Flask模型中的id字段主键设置默认uuid吗

如何在WSL2中更新Python到最新版本(3.12.2)?

什么是合并两个embrame的最佳方法,其中一个有日期范围,另一个有日期没有任何共享列?

Geopandas未返回正确的缓冲区(单位:米)

具有相同图例 colored颜色 和标签的堆叠子图

使用polars. pivot()旋转一个框架(类似于R中的pivot_longer)

python的文件. truncate()意外地没有截断'