IOError: 无法打开资源

2 投票
3 回答
9972 浏览
提问于 2025-05-01 13:53

我在ipython notebook上写了一段代码,想做一个词云,但出现了错误。从我看到的情况来看,这个错误似乎和我的代码无关(可能是字体的问题?)

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

在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'
)

这样就可以正常运行了!

0

在Mac OS X上,我遇到了这个错误:

IOError: cannot open resource

我在“wordcloud.py”这个文件里修改了FONT_PATH

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

然后错误就消失了。

0

你需要在你的词云对象中指定一个字体路径。这个库的作者Amueller在最新版本中做了很多改动。我根据这些改动更新了我的项目,你可以在这里查看

撰写回答