amueller / word_cloud

A little word cloud generator in Python
https://amueller.github.io/word_cloud
MIT License
10.02k stars 2.31k forks source link

Only supported for TrueType font #722

Closed mjcarter95 closed 1 year ago

mjcarter95 commented 1 year ago

Description

ValueError: Only supported for TrueType fonts when trying to create a WorldCloud on Ubuntu 22.04 with Python 3.10.6.

Steps/Code to Reproduce

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 is downloaded from https://www.freefontspro.com/14454/arial.ttf and placed in the same directory as the above Python code.

Expected Results

No error thrown, world cloud of "foo bar foo bar hello world" is produced and shown.

Actual Results

The following error:

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

Versions

Linux-5.19.0-38-generic-x86_64-with-glibc2.35
Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0]
NumPy 1.24.2
matplotlib 3.7.1
wordcoud 1.9.1.1
zd178 commented 1 year ago

Did you solve this problem, please?I tried to fix this yesterday, but I spent a long time trying a lot of fonts and it didn't work...

mjcarter95 commented 1 year ago

Did you solve this problem, please?I tried to fix this yesterday, but I spent a long time trying a lot of fonts and it didn't work...

A suggestion on StackOverflow solved the issue for me, see here.

libfreetype6 was installed on my system but (I assume) out of date, running

sudo apt install libfreetype6

fixed the error for me!

abubelinha commented 1 year ago

@mjcarter95 your issue is still relevant: could you reopen it or should I open a new one?

I have the same exact error message but no options to try apt I am on Windows 7, Python 3.8:

python3 -m PIL outputs:

c:\Python38\python -m PIL
--------------------------------------------------------------------
Pillow 9.1.1
Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit (AMD64)]
--------------------------------------------------------------------
Python modules loaded from C:\Users\abu\AppData\Local\Programs\Python\Python38\lib\site-packages\PIL
Binary modules loaded from C:\Users\abu\AppData\Local\Programs\Python\Python38\lib\site-packages\PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 9.1.1
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.12.1
--- LITTLECMS2 support ok, loaded 2.13.1
--- WEBP support ok, loaded 1.2.2
--- WEBP Transparency support ok
--- WEBPMUX support ok
--- WEBP Animation support ok
--- JPEG support ok, compiled for libjpeg-turbo 2.1.3
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.0
--- ZLIB (PNG/ZIP) support ok, loaded 1.2.12
--- LIBTIFF support ok, loaded 4.3.0
*** RAQM (Bidirectional Text) support not installed
*** LIBIMAGEQUANT (Quantization method) support not installed
*** XCB (X protocol) support not installed
--------------------------------------------------------------------
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
--------------------------------------------------------------------

Thanks @abubelinha

abubelinha commented 1 year ago

OK, the same problem reproduced also in a different Windows 10 machine, Python 3.9.9, Pillow 9.0.1.

So I tried this (alternative answer to the same SO question above):

pip install --upgrade pillow

That upgraded Pillow to version 9.5.0 and solved my problem in both Windows machines

sunzhicheng369 commented 1 year ago

Problem solved, thank you

kirtijain627 commented 1 year ago

Pillow version is 9.5.0, python- 3.10.5. Still the problem is not solved

talha2k commented 1 year ago

OK, the same problem reproduced also in a different Windows 10 machine, Python 3.9.9, Pillow 9.0.1.

So I tried this (alternative answer to the same SO question above):

pip install --upgrade pillow

That upgraded Pillow to version 9.5.0 and solved my problem in both Windows machines

This doesn't solve the problem on windows. Pillow version is 9.5.0, python- 3.10.0. Anaconda Enviornemnt Restarted. Windows Restarted. Double checked that font file exists and is ttf. Still the problem is not solved.