Search code examples
python-imaging-libraryword-cloudwordcloud2

Wordcloud installation on Windows (PIL, pillow)


Basically, I am trying to generate simple word cloud using Python on Windows. Hence, I have installed wordcloud as:

pip install wordcloud

I am trying to run the simplest example from here as below:

import os

from os import path
from wordcloud import WordCloud

# get data directory (using getcwd() is needed to support running example in generated IPython notebook)
d = path.dirname(__file__) if "__file__" in locals() else os.getcwd()

# Read the whole text.
text = open(path.join(d, 'constitution.txt')).read()

# Generate a word cloud image
wordcloud = WordCloud().generate(text)

# Display the generated image:
# the matplotlib way:
import matplotlib.pyplot as plt
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")

# lower max_font_size
wordcloud = WordCloud(max_font_size=40).generate(text)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()

The error I got:

Traceback (most recent call last):
  File "C:\Users\XXXXX\test_wordcloud.py", line 13, in <module>
    wordcloud = WordCloud().generate(text)
  File "C:\Anaconda3\lib\site-packages\wordcloud\wordcloud.py", line 639, in generate
    return self.generate_from_text(text)
  File "C:\Anaconda3\lib\site-packages\wordcloud\wordcloud.py", line 621, in generate_from_text
    self.generate_from_frequencies(words)
  File "C:\Anaconda3\lib\site-packages\wordcloud\wordcloud.py", line 454, in generate_from_frequencies
    max_font_size=self.height)
  File "C:\Anaconda3\lib\site-packages\wordcloud\wordcloud.py", line 503, in generate_from_frequencies
    font = ImageFont.truetype(self.font_path, font_size)
  File "C:\Anaconda3\lib\site-packages\PIL\ImageFont.py", line 959, in truetype
    return freetype(font)
  File "C:\Anaconda3\lib\site-packages\PIL\ImageFont.py", line 956, in freetype
    return FreeTypeFont(font, size, index, encoding, layout_engine)
  File "C:\Anaconda3\lib\site-packages\PIL\ImageFont.py", line 219, in __init__
    if core.HAVE_RAQM:
  File "C:\Anaconda3\lib\site-packages\PIL\ImageFont.py", line 58, in __getattr__
    raise ImportError("The _imagingft C module is not installed")
ImportError: The _imagingft C module is not installed

The PIL and Python versions on my side are:

from PIL import Image, ImageDraw, ImageFilter, ImageFont
print('PIL',PIL.__version__)
import sys
print(sys.version)

as:

PIL 9.2.0
3.7.13 (default, Mar 28 2022, 08:03:21) [MSC v.1916 64 bit (AMD64)]

Any suggestions ? Thanks


Solution

  • On Windows 10, install the FreeType binding. Hopefully, it is resolved by re-installing the Pillow with disabling the pip cache as:

    pip install freetype-py
    
    pip uninstall pillow
    pip install --no-cache-dir pillow
    

    Once pillow is recompiled, everything worked properly for me! In addition, the following check:

    from PIL import features
    print(features.check('freetype2'))
    

    was False before the recent PIL re-installation. However, it is converted into True after above listed steps. By the way, no need to reboot!

    enter image description here