Search code examples
pythonfontsioword-cloud

IOError: cannot open resource


I have a code on ipython notebook where I am attempting to make a wordcloud, but I get an error. From what I could see, the error is related to something else and not my code (something with fonts?)

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

I'm not so sure as to how to interpret this. Seems like a class I'm using needs some fonts, and something happens with some font files. Any insight will be appreciated

I am using this as a base: http://99projects.co/reports/p05-word-frequency/


Solution

  • You'll need to specify a font_path in your wordcloud object. Amueller, the author of the library changed a lot of things in his latest version. I updated my project according to these changes, you can check it here