WordCloud Only Supported for TrueType fonts
Asked Answered
U

5

10

I am trying to generate a word cloud using the WordCloud module in Python, however I see the following error whenever I call .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

As it stands, I am trying to create a very simple example 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()

Where arial.ttf is downloaded from https://www.freefontspro.com/14454/arial.ttf and placed in the same directory as test.py. I am using Ubuntu 22.04 and Python 3.10.6.

I was expecting to generate a word cloud from the input "foo bar foo bar hello world", however see the error ValueError: Only supported for TrueType fonts despite passing a ttf to the font_path argument.

Unrefined answered 28/4, 2023 at 12:16 Comment(0)
I
2

I could not reproduce your error with Python 3.10 even after downloading the same font - though I am on macOS.

The only thing I can imagine is that your PIL doesn't support TrueType fonts, so you can check with:

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

Or, more speedily - since we are only interested in "freetype" lines:

python3 -m PIL | grep -i type

Then you may need to install freetype with a command something like - not too sure of the exact package name:

sudo apt install libfreetype6
Inflammation answered 29/4, 2023 at 9:37 Comment(2)
Perfect! My libfreetype6 was out of date and needed to be manually updated!Unrefined
I had an identical error message in several Windows machines (Win7, Python 3.8.7, Pillow 9.1.1 & Win10, Python 3.9.9, Pillow 9.0.1), both with python3 -m PIL output: --- FREETYPE2 support ok. The solution was upgrading Pillow to 9.5.0: pip install --upgrade pillow as @Singlet suggests in another answerPhoneme
S
15

These work for me:

pip install --upgrade pip 

pip install --upgrade Pillow

Tell me if it doesn't work

Singlet answered 29/4, 2023 at 10:18 Comment(4)
I tried installing/upgrading pillow but am still running into the same problemUnrefined
This worked for me, although I overlooked your answer and tried first the accepted one. Thanks! BTW, not sure if this is relevant, but in my case (Windows) pillow package name is in lowercasePhoneme
Seems like pillow is the solution for Windows and libfreetype6 for Ubuntu. I've kept Mark's answer as accepted as it helped in my case.Unrefined
I ran into the same problem after updating word cloud. In my context (jupyter notebook started from a WSL shell) none of the workarounds helped unfortunately. libfreetype6 is up to date and I updated pillow to version 9.5.0. However, I still get the same error message.Sferics
I
2

I could not reproduce your error with Python 3.10 even after downloading the same font - though I am on macOS.

The only thing I can imagine is that your PIL doesn't support TrueType fonts, so you can check with:

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

Or, more speedily - since we are only interested in "freetype" lines:

python3 -m PIL | grep -i type

Then you may need to install freetype with a command something like - not too sure of the exact package name:

sudo apt install libfreetype6
Inflammation answered 29/4, 2023 at 9:37 Comment(2)
Perfect! My libfreetype6 was out of date and needed to be manually updated!Unrefined
I had an identical error message in several Windows machines (Win7, Python 3.8.7, Pillow 9.1.1 & Win10, Python 3.9.9, Pillow 9.0.1), both with python3 -m PIL output: --- FREETYPE2 support ok. The solution was upgrading Pillow to 9.5.0: pip install --upgrade pillow as @Singlet suggests in another answerPhoneme
A
2

I wanted to respond because above solutions did not work for me and I wasn't reading the errors.

Don't forget to try:

pip install **--user** --upgrade pip
pip install **--user** --upgrade Pillow

in case you had an error saying you didn't have the right permissions to upgrade.

Autogiro answered 21/5 at 7:45 Comment(0)
H
1

Using Python 3.6, re-installed wordcloud 1.8.0 (pip install wordcloud==1.8.0), issue fixed. Latest version of WordCloud does not support Python3.6 as per official docs.

Heall answered 4/7, 2023 at 6:11 Comment(0)
C
1

tried this solution 1

upgrading PIP and Pillow didn't work for me like many of others

tried this solution 2 - worked very well for me

Using Python 3.6, re-installed wordcloud 1.8.0 (pip install wordcloud==1.8.0), issue fixed. Latest version of WordCloud does not support Python3.6 as per official docs.

Cankerous answered 29/9, 2023 at 15:34 Comment(1)
This should be a comment on existing answers. Do not use "answers" unless you have a new answer, that wasn't already given, to bring. Especially not to testify that this given answers works or doesn't work for you.El

© 2022 - 2024 — McMap. All rights reserved.