IOError:无法打开资源

时间:2014-11-04 15:25:09

标签: python fonts io word-cloud

我在ipython笔记本上有一个代码,我试图制作一个wordcloud,但是我收到了一个错误。从我所看到的,错误与其他东西有关,而不是我的代码(有字体的东西?)

---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-71-795b92e06f22> in <module>()
  4 #    print x[0], x[1]
  5 
----> 6 genWordCloud(list_of_tuples)

<ipython-input-70-55c34fb2c915> in genWordCloud(filename)
 15     count = filename
 16     #words = myWC.fit_words(count, width=500, height=500)
---> 17     words = myWC.fit_words(count)
 18     myWC.draw(words, pngPath + os.path.splitext(filename)[0] + '.png', width=500, height=500, scale=1)
 19     return 'Cloud generated for {}'.format(filename)

/usr/local/lib/python2.7/dist-packages/wordcloud/wordcloud.pyc in fit_words(self, words)
177             while True:
178                 # try to find a position
--> 179                 font = ImageFont.truetype(self.font_path, font_size)
180                 # transpose font optionally
181                 if random_state.random() < self.prefer_horizontal:

/usr/local/lib/python2.7/dist-packages/PIL/ImageFont.pyc in truetype(font, size, index, encoding, filename)
238 
239     try:
--> 240         return FreeTypeFont(font, size, index, encoding)
241     except IOError:
242         if sys.platform == "win32":

/usr/local/lib/python2.7/dist-packages/PIL/ImageFont.pyc in __init__(self, font, size, index, encoding, file)
135 
136         if isPath(font):
--> 137             self.font = core.getfont(font, size, index, encoding)
138         else:
139             self.font_bytes = font.read()

IOError: cannot open resource

我不太清楚如何解释这一点。看起来像我使用的类需要一些字体,而某些字体文件会发生一些事情。任何见解将不胜感激

我使用它作为基础:http://99projects.co/reports/p05-word-frequency/

3 个答案:

答案 0 :(得分:0)

您需要在wordcloud对象中指定font_path。 the library的作者Amueller在他的最新版本中改变了很多东西。我根据这些更改更新了我的项目,您可以check it here

答案 1 :(得分:0)

在Mac OS X上,我收到了错误:

IOError: cannot open resource

我更改了文件中的FONT_PATH&#34; wordcloud.py&#34;

FONT_PATH = os.environ.get("FONT_PATH", "/Library/Fonts/Times New Roman.ttf")

错误就消失了。

答案 2 :(得分:0)

在Mac OS X上,出现错误:

IOError: cannot open resource

我在文件“ xxx.py”中更改了FONT_PATH

 wc = WordCloud(
    max_words=100,
    width=2000,
    height=1200,
    font_path = 'Hiragino Sans GB.ttc'
)

可能会正确!