atizo / PyTagCloud

Create beautiful tag clouds as images or HTML
https://labs.atizo.com/software/#pytagcloud
BSD 3-Clause "New" or "Revised" License
397 stars 128 forks source link

font issue with pygame #6

Open rblakemesser opened 12 years ago

rblakemesser commented 12 years ago

This might not be a pytagcloud issue per se... but here goes.

I'm getting a IO error related to the font filenames. I assume it's a pygame problem:

Traceback (most recent call last): File "/home/blake/Dropbox/2012.03_WorkCode/tagcloud.py", line 41, in File "/usr/local/lib/python2.7/dist-packages/pytagcloud/init.py", line 344, in create_tag_image File "/usr/local/lib/python2.7/dist-packages/pytagcloud/init.py", line 275, in _draw_cloud File "/usr/local/lib/python2.7/dist-packages/pytagcloud/init.py", line 62, in init IOError: unable to read font filename

When I run the example code off your site, it runs though without a problem. I just can't figure out for the life of me why my code, which uses the same font as the example, won't run.

Here's my code......

import csv from pytagcloud import create_tag_image, make_tags from pytagcloud.lang.counter import get_tag_counts

input_csv = csv.reader(open('sql_output.csv','rb'),delimiter="~")

....[some stuff here that creates a list of word frequency tuples from my csv data]

word_frequencies = [] for n,irecord in enumerate(sorted(word_freqs.items(), key=lambda item: item[1])): word_frequencies.append(tuple([irecord[0],irecord[1]]))

tags = make_tags(word_frequencies, maxsize=120)

create_tag_image(tags, 'cloud_large2.png', size=(900, 600), fontname='Lobster')

rblakemesser commented 12 years ago

More investigation shows that the error is only thrown if the size of the word_frequencies list is too large. For example, when I limit it to 300 words, then the font error is not thrown. I wonder if somewhere in the middle of my list there are special characters that are not available in the packaged fonts...?

alourenco commented 12 years ago

I had a similar error when adapting the example to my own problem:

In [7]: Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1413, in call File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 498, in callit File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 263, in idle_draw File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 248, in draw File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 394, in draw File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 798, in draw File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 1946, in draw File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 1017, in draw File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 234, in draw File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper File "/usr/lib/pymodules/python2.7/matplotlib/text.py", line 526, in draw File "/usr/lib/pymodules/python2.7/matplotlib/text.py", line 300, in _get_layout File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 180, in get_text_width_height_descent File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 221, in _get_agg_font RuntimeError: Could not open facefile /usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf; Cannot_Open_Resource

Here is my code: print "ploting tag-clouds..." size=(900, 600) for i in range(k): print "tag:",i t=[(vocabNew2[d],vocabWeight[i,d]) for d in range(len(vocabNew2))] t=(tuple(t)) tags = make_tags(t, maxsize=120) create_tag_image(tags, fileName + 'cloud_cluster-'+str(i)+'.png', size=size, fontname='Nobile')

I will try to limit the number of tokens...

aeby commented 12 years ago

Can you check if https://github.com/atizo/PyTagCloud/commits/master8361a4d1f3a1a22a87b23f0787242d0ac6181618 fixes your problem? Thanks!

alourenco commented 12 years ago

Thank you aeby, I will check it!

ila987 commented 11 years ago

Hello everybody!

I have the get the same error when I give as input a text sequence too big:

IOError: unable to read font file '/pytagcloud-0.3.5/pytagcloud/fonts/nobile.ttf'

I modified init.py with b19d236, but I still get the same error..

Any help is really appreciated! Thanks

arianpasquali commented 11 years ago

Same error here. Still can't find a way to workaround this issue.

create_tag_image(tags, str(cluster_id) + '_wordcloud.png', size=(900, 700))

Traceback (most recent call last): File "text_clustering.py", line 292, in File "text_clustering.py", line 257, in main File "text_clustering.py", line 226, in perform_clustering File "text_clustering.py", line 59, in plot_wordclouds File "/Library/Python/2.7/site-packages/pytagcloud/init.py", line 344, in create_tag_image File "/Library/Python/2.7/site-packages/pytagcloud/init.py", line 275, in _draw_cloud File "/Library/Python/2.7/site-packages/pytagcloud/init.py", line 62, in init IOError: unable to read font file '/Library/Python/2.7/site-packages/pytagcloud/fonts/DroidSans.ttf'

ghost commented 11 years ago

Still getting this same error. Is there a solution anywhere?

srirajan commented 10 years ago

From what I gathered, this seems to be a problem with input data. I tested with normal words and was able to run it against 1000s of words but it does fail with that error for some data. Not sure what trips it though.

fnielsen commented 10 years ago

The problem seems to be too many open files. As create_tag_image is called again and again the number of font file references increases. In my Ubuntu system I can see the open files in /proc/PID/fd/ where PID is the process identifier for the python process. I believe it is around 1000 open files that I run into troubles. My ulimit -a tells me I "open file" "1024" which fits well with my observation. For small examples like in the README.rst the number of files only increases a little each time, but with a larger text the number of files in /proc/PID/fd increases quickly, e.g.:

from nltk.corpus import brown
tags = make_tags(get_tag_counts(" ".join(brown.words()[:1000])))
create_tag_image(tags, 'cloud_large.png', size=(900, 600), fontname='Lobster')

The number of files fits well with the size of the FONTS variable. However, del FONTS, pygame.quit(), pygame.font.quit(), del pygame or del pytagcloud do not help.

fnielsen commented 9 years ago

An evil workaround is suggested here: https://stackoverflow.com/questions/25944211/how-do-i-close-files-in-python-when-i-havent-got-the-file-identifiers

smagnan commented 8 years ago

I had this error appearing randomly in a cloud generation loop (independent of which cloud was generated it seems)

building word cloud for user BBC News (World)
word cloud built
building word cloud for user Human Rights Watch
word cloud built
building word cloud for user InThePanda
Traceback (most recent call last):
  File "main.py", line 217, in <module>
  File "main.py", line 181, in analyse_twitter_data
  File "main.py", line 133, in atd_word_cloud
  File "main.py", line 121, in build_cloud
  File "/home/smagnan/Documents/365Talents/Twitter/user.py", line 41, in word_cloud
  File "/usr/local/lib/python2.7/dist-packages/pytagcloud/__init__.py", line 344, in create_tag_image
  File "/usr/local/lib/python2.7/dist-packages/pytagcloud/__init__.py", line 275, in _draw_cloud
  File "/usr/local/lib/python2.7/dist-packages/pytagcloud/__init__.py", line 62, in __init__
IOError: unable to read font filename

After investigation it also seemed to me it was too many open files as stated by @fnielsen The evil workaround solved this issue (see previous post), but sometimes causes python to crash when exiting my program ... Anyway, this workaround seems to be working until the issue is solved