amueller / word_cloud

A little word cloud generator in Python
https://amueller.github.io/word_cloud
MIT License
10.15k stars 2.32k forks source link

OSError: cannot open resource #450

Open diegoelages opened 5 years ago

diegoelages commented 5 years ago

Description

Error: OSError: cannot open resource

Steps/Code to Reproduce

from PIL import Image import numpy as np from wordcloud import WordCloud

words = 'access guest guest apartment area area bathroom bed bed bed bed bed bedroom block coffee coffee coffee coffee entrance entry francisco free garden guest home house kettle kettle kitchen kitchen kitchen kitchen kitchen kitchenliving located microwave neighborhood new park parking place privacy private queen room san separate seperate shared space space space street suite time welcome' scale = 12 mask = Image.open('c:/temp/images.png') width, height = mask.size mask = np.array(mask.resize((int(mask.size[0] / float(scale)), int(mask.size[1] / float(scale))))) wordcloud = WordCloud(scale=scale, background_color='white', width=int(width/float(scale)), height=int(height/float(scale)), margin=2, font_path='msyh.ttc', mask=mask).generate(words) wordcloud.to_file('c:/temp/out.png')

Expected Results

No erro :-)

Actual Results

PS D:\Diego\ProjetosPython\Aprendizado-master> python cloud2.py Traceback (most recent call last): File "cloud2.py", line 10, in wordcloud = WordCloud(scale=scale, background_color='white', width=int(width/float(scale)), height=int(height/float(scale)), margin=2, font_path='msyh.ttc', mask=mask).generate(words) File "D:\Usuários\dlsan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\wordcloud\wordcloud.py", line 605, in generate return self.generate_from_text(text) File "D:\Usuários\dlsan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\wordcloud\wordcloud.py", line 587, in generate_from_text self.generate_from_frequencies(words) File "D:\Usuários\dlsan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\wordcloud\wordcloud.py", line 426, in generate_from_frequencies max_font_size=self.height) File "D:\Usuários\dlsan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\wordcloud\wordcloud.py", line 473, in generate_from_frequencies font = ImageFont.truetype(self.font_path, font_size) File "D:\Usuários\dlsan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\PIL\ImageFont.py", line 280, in truetype return FreeTypeFont(font, size, index, encoding, layout_engine) File "D:\Usuários\dlsan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\PIL\ImageFont.py", line 145, in init layout_engine=layout_engine) OSError: cannot open resource

Versions

Windows-7-6.1.7601-SP1 Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] NumPy 1.15.3 matplotlib 3.0.1 wordcoud 1.5.0

diegoelages commented 5 years ago

images

amueller commented 5 years ago

looks like it can't open the font. Can you check if "D:\Usuários\dlsan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\wordcloud\DroidSansMono.ttf" exists? Can you also check what wordcloud.font_path is?

And just in case I'm wrong, can you please also see what happens if you remove the last line?

hugovk commented 5 years ago

This looks like a bug in Pillow on Windows with paths containing non-ASCII characters, from the accented á in Usuários.

A workaround is to copy the font to a path that contains only ASCII characters.

See https://github.com/python-pillow/Pillow/issues/3145.

amueller commented 5 years ago

@hugovk good catch!