python中的Wordcloud错误

时间:2016-05-13 04:28:57

标签: python python-imaging-library

您好我正在尝试在Python中生成我的文本文件searches.txt的wordcliud。这是我的代码:

from os import path
from wordcloud import WordCloud
import matplotlib.pyplot as plt

d = path.dirname(__file__)


text = open(path.join(d, 'search.txt')).read()


wordcloud = WordCloud().generate(text)



plt.imshow(wordcloud)
plt.axis("off")

wordcloud = WordCloud(max_font_size=40, relative_scaling=.5).generate(text)
plt.figure()
plt.imshow(wordcloud)
plt.axis("off")
plt.show()

这是错误::

Traceback (most recent call last):
  File "C:\Python27\wordcloud12.py", line 4, in <module>
    from wordcloud import WordCloud
  File "C:\Python27\lib\site-packages\wordcloud\__init__.py", line 1, in <module>
    from .wordcloud import WordCloud, STOPWORDS, random_color_func, get_single_color_func
  File "C:\Python27\lib\site-packages\wordcloud\wordcloud.py", line 17, in <module>
    from PIL import Image
ImportError: No module named PIL

1 个答案:

答案 0 :(得分:0)

您应首先安装PIL(或Pillow):

pip install pillow

请注意,在Windows环境中安装PIL / Pillow可能很困难。从以下位置下载安装程序包更容易:

http://www.lfd.uci.edu/~gohlke/pythonlibs/#pillow

然后使用pip安装它,例如:

pip install Pillow-3.2.0-cp27-cp27m-win_amd64.whl