1. "OSError: cannot open resource"が出たとき
Python3
>>> from bs4 import BeautifulSoup
>>> import requests
>>> from wordcloud import WordCloud
>>> text = 'なにか適当な文章。この文章は解析対象です。'
>>> import MeCab
>>> tagger = MeCab.Tagger ("-Owakati")
>>> words = tagger.parse (text)
>>> print(words)
なにか 適当 な 文章 。 この 文章 は 解析 対象 です 。
>>> print(type(words))
<class 'str'>
>>>
>>> fpath='/System/Library/Fonts/ヒラギノ明朝 ProN.ttc'
>>> stop_words = ['てる', 'いる', 'なる', 'れる', 'する', 'ある', 'こと', 'これ', 'さん', 'して']
>>> wordcloud = WordCloud(background_color="red", colormap="winter",width=900, height=500, font_path=fpath, stopwords=set(stop_words)).generate(words)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/wordcloud/wordcloud.py", line 632, in generate
return self.generate_from_text(text)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/wordcloud/wordcloud.py", line 614, in generate_from_text
self.generate_from_frequencies(words)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/wordcloud/wordcloud.py", line 446, in generate_from_frequencies
self.generate_from_frequencies(dict(frequencies[:2]),
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/wordcloud/wordcloud.py", line 496, in generate_from_frequencies
font = ImageFont.truetype(self.font_path, font_size)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/PIL/ImageFont.py", line 836, in truetype
return freetype(font)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/PIL/ImageFont.py", line 833, in freetype
return FreeTypeFont(font, size, index, encoding, layout_engine)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/PIL/ImageFont.py", line 193, in __init__
self.font = core.getfont(
OSError: cannot open resource
>>>
2. 解決策: 使用中のPCまたはサーバの文字フォントをTerminalで確認する
Terminal上で、find /Library/Fonts/ と入力して確認します。
( 参考 )
:wordcloud macでフォント指定 pythonで動かす
・2018/6/26【4日目】Word-cloudを日本語に対応させる
私のPCの場合は、Library/Fonts//ヒラギノ丸ゴ ProN W4.ttcだった。
Terminal
$ find /Library/Fonts/
/Library/Fonts/
/Library/Fonts//Al Nile.ttc
/Library/Fonts//Al Tarikh.ttc
/Library/Fonts//AlBayan.ttc
/Library/Fonts//AmericanTypewriter.ttc
/Library/Fonts//Andale Mono.ttf
/Library/Fonts//Apple Chancery.ttf
/Library/Fonts//AppleGothic.ttf
/Library/Fonts//AppleMyungjo.ttf
/Library/Fonts//Arial Black.ttf
/Library/Fonts//Arial Bold Italic.ttf
/Library/Fonts//Arial Bold.ttf
/Library/Fonts//Arial Italic.ttf
/Library/Fonts//Arial Narrow Bold Italic.ttf
/Library/Fonts//Arial Narrow Bold.ttf
/Library/Fonts//Arial Narrow Italic.ttf
/Library/Fonts//Arial Narrow.ttf
/Library/Fonts//Arial Rounded Bold.ttf
/Library/Fonts//Arial Unicode.ttf
/Library/Fonts//Arial.ttf
/Library/Fonts//Athelas.ttc
/Library/Fonts//Ayuthaya.ttf
/Library/Fonts//Baghdad.ttc
/Library/Fonts//Bangla MN.ttc
/Library/Fonts//Bangla Sangam MN.ttc
/Library/Fonts//Baoli.ttc
/Library/Fonts//Baskerville.ttc
/Library/Fonts//Beirut.ttc
/Library/Fonts//BigCaslon.ttf
/Library/Fonts//Bodoni 72 OS.ttc
/Library/Fonts//Bodoni 72 Smallcaps Book.ttf
/Library/Fonts//Bodoni 72.ttc
/Library/Fonts//Bodoni Ornaments.ttf
/Library/Fonts//Bradley Hand Bold.ttf
/Library/Fonts//Brush Script.ttf
/Library/Fonts//Chalkboard.ttc
/Library/Fonts//ChalkboardSE.ttc
/Library/Fonts//Chalkduster.ttf
/Library/Fonts//Charter.ttc
/Library/Fonts//Cochin.ttc
/Library/Fonts//Comic Sans MS Bold.ttf
/Library/Fonts//Comic Sans MS.ttf
/Library/Fonts//Copperplate.ttc
/Library/Fonts//Corsiva.ttc
/Library/Fonts//Courier New Bold Italic.ttf
/Library/Fonts//Courier New Bold.ttf
/Library/Fonts//Courier New Italic.ttf
/Library/Fonts//Courier New.ttf
/Library/Fonts//Damascus.ttc
/Library/Fonts//DecoTypeNaskh.ttc
/Library/Fonts//Devanagari Sangam MN.ttc
/Library/Fonts//DevanagariMT.ttc
/Library/Fonts//Didot.ttc
/Library/Fonts//DIN Alternate Bold.ttf
/Library/Fonts//DIN Condensed Bold.ttf
/Library/Fonts//Diwan Kufi.ttc
/Library/Fonts//Diwan Thuluth.ttf
/Library/Fonts//encodings.dir
/Library/Fonts//EuphemiaCAS.ttc
/Library/Fonts//Farah.ttc
/Library/Fonts//Farisi.ttf
/Library/Fonts//fonts.dir
/Library/Fonts//fonts.list
/Library/Fonts//fonts.scale
/Library/Fonts//Futura.ttc
/Library/Fonts//Georgia Bold Italic.ttf
/Library/Fonts//Georgia Bold.ttf
/Library/Fonts//Georgia Italic.ttf
/Library/Fonts//Georgia.ttf
/Library/Fonts//GillSans.ttc
/Library/Fonts//Gujarati Sangam MN.ttc
/Library/Fonts//GujaratiMT.ttc
/Library/Fonts//Gungseouche.ttf
/Library/Fonts//Gurmukhi MN.ttc
/Library/Fonts//Gurmukhi Sangam MN.ttc
/Library/Fonts//Gurmukhi.ttf
/Library/Fonts//Hannotate.ttc
/Library/Fonts//Hanzipen.ttc
/Library/Fonts//HeadlineA.ttf
/Library/Fonts//Herculanum.ttf
/Library/Fonts//Hoefler Text Ornaments.ttf
/Library/Fonts//Hoefler Text.ttc
/Library/Fonts//Impact.ttf
/Library/Fonts//InaiMathi.ttf
/Library/Fonts//Iowan Old Style.ttc
/Library/Fonts//ITFDevanagari.ttc
/Library/Fonts//Kailasa.ttc
/Library/Fonts//Kaiti.ttc
/Library/Fonts//Kannada MN.ttc
/Library/Fonts//Kannada Sangam MN.ttc
/Library/Fonts//Kefa.ttc
/Library/Fonts//Khmer MN.ttc
/Library/Fonts//Khmer Sangam MN.ttf
/Library/Fonts//Klee.ttc
/Library/Fonts//Kokonor.ttf
/Library/Fonts//Krungthep.ttf
/Library/Fonts//KufiStandardGK.ttc
/Library/Fonts//Lantinghei.ttc
/Library/Fonts//Lao MN.ttc
/Library/Fonts//Lao Sangam MN.ttf
/Library/Fonts//Libian.ttc
/Library/Fonts//Luminari.ttf
/Library/Fonts//Malayalam MN.ttc
/Library/Fonts//Malayalam Sangam MN.ttc
/Library/Fonts//Marion.ttc
/Library/Fonts//Microsoft Sans Serif.ttf
/Library/Fonts//Mishafi Gold.ttf
/Library/Fonts//Mishafi.ttf
/Library/Fonts//Mshtakan.ttc
/Library/Fonts//Muna.ttc
/Library/Fonts//Myanmar MN.ttc
/Library/Fonts//Myanmar Sangam MN.ttc
/Library/Fonts//Nadeem.ttc
/Library/Fonts//NanumGothic.ttc
/Library/Fonts//NanumMyeongjo.ttc
/Library/Fonts//NanumScript.ttc
/Library/Fonts//NewPeninimMT.ttc
/Library/Fonts//NISC18030.ttf
/Library/Fonts//Oriya MN.ttc
/Library/Fonts//Oriya Sangam MN.ttc
/Library/Fonts//Osaka.ttf
/Library/Fonts//OsakaMono.ttf
/Library/Fonts//Papyrus.ttc
/Library/Fonts//PCmyoungjo.ttf
/Library/Fonts//Phosphate.ttc
/Library/Fonts//Pilgiche.ttf
/Library/Fonts//PlantagenetCherokee.ttf
/Library/Fonts//PTMono.ttc
/Library/Fonts//PTSans.ttc
/Library/Fonts//PTSerif.ttc
/Library/Fonts//PTSerifCaption.ttc
/Library/Fonts//Raanana.ttc
/Library/Fonts//Sana.ttc
/Library/Fonts//Sathu.ttf
/Library/Fonts//Savoye LET.ttc
/Library/Fonts//Seravek.ttc
/Library/Fonts//Shree714.ttc
/Library/Fonts//SignPainter.otf
/Library/Fonts//Silom.ttf
/Library/Fonts//Sinhala MN.ttc
/Library/Fonts//Sinhala Sangam MN.ttc
/Library/Fonts//Skia.ttf
/Library/Fonts//SnellRoundhand.ttc
/Library/Fonts//Songti.ttc
/Library/Fonts//STIXGeneral.otf
/Library/Fonts//STIXGeneralBol.otf
/Library/Fonts//STIXGeneralBolIta.otf
/Library/Fonts//STIXGeneralItalic.otf
/Library/Fonts//STIXIntDBol.otf
/Library/Fonts//STIXIntDReg.otf
/Library/Fonts//STIXIntSmBol.otf
/Library/Fonts//STIXIntSmReg.otf
/Library/Fonts//STIXIntUpBol.otf
/Library/Fonts//STIXIntUpDBol.otf
/Library/Fonts//STIXIntUpDReg.otf
/Library/Fonts//STIXIntUpReg.otf
/Library/Fonts//STIXIntUpSmBol.otf
/Library/Fonts//STIXIntUpSmReg.otf
/Library/Fonts//STIXNonUni.otf
/Library/Fonts//STIXNonUniBol.otf
/Library/Fonts//STIXNonUniBolIta.otf
/Library/Fonts//STIXNonUniIta.otf
/Library/Fonts//STIXSizFiveSymReg.otf
/Library/Fonts//STIXSizFourSymBol.otf
/Library/Fonts//STIXSizFourSymReg.otf
/Library/Fonts//STIXSizOneSymBol.otf
/Library/Fonts//STIXSizOneSymReg.otf
/Library/Fonts//STIXSizThreeSymBol.otf
/Library/Fonts//STIXSizThreeSymReg.otf
/Library/Fonts//STIXSizTwoSymBol.otf
/Library/Fonts//STIXSizTwoSymReg.otf
/Library/Fonts//STIXVar.otf
/Library/Fonts//STIXVarBol.otf
/Library/Fonts//SukhumvitSet.ttc
/Library/Fonts//SuperClarendon.ttc
/Library/Fonts//Tahoma Bold.ttf
/Library/Fonts//Tahoma.ttf
/Library/Fonts//Tamil MN.ttc
/Library/Fonts//Tamil Sangam MN.ttc
/Library/Fonts//Telugu MN.ttc
/Library/Fonts//Telugu Sangam MN.ttc
/Library/Fonts//Times New Roman Bold Italic.ttf
/Library/Fonts//Times New Roman Bold.ttf
/Library/Fonts//Times New Roman Italic.ttf
/Library/Fonts//Times New Roman.ttf
/Library/Fonts//Trattatello.ttf
/Library/Fonts//Trebuchet MS Bold Italic.ttf
/Library/Fonts//Trebuchet MS Bold.ttf
/Library/Fonts//Trebuchet MS Italic.ttf
/Library/Fonts//Trebuchet MS.ttf
/Library/Fonts//TsukushiAMaruGothic.ttc
/Library/Fonts//TsukushiBMaruGothic.ttc
/Library/Fonts//Verdana Bold Italic.ttf
/Library/Fonts//Verdana Bold.ttf
/Library/Fonts//Verdana Italic.ttf
/Library/Fonts//Verdana.ttf
/Library/Fonts//Waseem.ttc
/Library/Fonts//WawaSC-Regular.otf
/Library/Fonts//WawaTC-Regular.otf
/Library/Fonts//Webdings.ttf
/Library/Fonts//WeibeiSC-Bold.otf
/Library/Fonts//WeibeiTC-Bold.otf
/Library/Fonts//Wingdings 2.ttf
/Library/Fonts//Wingdings 3.ttf
/Library/Fonts//Wingdings.ttf
/Library/Fonts//Xingkai.ttc
/Library/Fonts//Yu Gothic Bold.otf
/Library/Fonts//Yu Gothic Medium.otf
/Library/Fonts//Yuanti.ttc
/Library/Fonts//YuMincho.ttc
/Library/Fonts//YuppySC-Regular.otf
/Library/Fonts//YuppyTC-Regular.otf
/Library/Fonts//Zapfino.ttf
/Library/Fonts//ヒラギノ丸ゴ ProN W4.ttc
/Library/Fonts//儷宋 Pro.ttf
/Library/Fonts//儷黑 Pro.ttf
/Library/Fonts//华文仿宋.ttf
/Library/Fonts//华文细黑.ttf
/Library/Fonts//华文黑体.ttf
$
以下で実行成功
Python3
>>> fpath = "/Library/Fonts//ヒラギノ丸ゴ ProN W4.ttc"
>>> wordcloud = WordCloud(background_color="red", colormap="winter",width=900, height=500, font_path=fpath, stopwords=set(stop_words)).generate(words)
>>> print(wordcloud)
<wordcloud.wordcloud.WordCloud object at 0x1066bd5e0>
>>>
>>> import matplotlib.pyplot as plt
>>> plt.figure(figsize=(4,4))
>>> plt.imshow(wordcloud)
>>> plt.axis("off")
>>> plt.show()
>>> wordcloud.to_file('wc1.png')
<wordcloud.wordcloud.WordCloud object at 0x1066bd5e0>
>>> quit()
( 水色で描画 )
Python3
>>> wordcloud = WordCloud(background_color="skyblue", colormap="winter",width=900, height=500, font_path=fpath, stopwords=set(stop_words)).
>>> plt.imshow(wordcloud)
<matplotlib.image.AxesImage object at 0x11d0b4fd0>
>>> plt.show() >>>
( 黄色で描画 )
Python3
>>> color = "yellow"
>>> wordcloud = WordCloud(background_color=color, colormap="winter",width=900, height=500, font_path=fpath, stopwords=set(stop_words)).
>>> plt.imshow(wordcloud)
<matplotlib.image.AxesImage object at 0x1211afe50>
>>> plt.show()
>>>