wordcloud AttributeError: 'TransposedFont' object has no attribute 'getbbox'

5k views Asked by At

I tried to run the following code to generate a word cloud.

text = 'word1 word2 word2 word3 word3 word3'

from wordcloud import WordCloud

wordcloud = WordCloud(width=480, height=480).generate(text)

But I ran into this error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-17-659d2fbc8555> in <module>
     8 
     9 # Generate the word cloud
---> 10 wordcloud.generate(text)
    11 
    12 # Display the word cloud

~\anaconda3\lib\site-packages\wordcloud\wordcloud.py in generate(self, text)
   637         self
   638         """
--> 639         return self.generate_from_text(text)
   640 
   641     def _check_generated(self):

~\anaconda3\lib\site-packages\wordcloud\wordcloud.py in generate_from_text(self, text)
   619         """
   620         words = self.process_text(text)
--> 621         self.generate_from_frequencies(words)
   622         return self
   623 

~\anaconda3\lib\site-packages\wordcloud\wordcloud.py in generate_from_frequencies(self, frequencies, max_font_size)
   451                 font_size = self.height
   452             else:
--> 453                 self.generate_from_frequencies(dict(frequencies[:2]),
   454                                                max_font_size=self.height)
   455                 # find font sizes

~\anaconda3\lib\site-packages\wordcloud\wordcloud.py in generate_from_frequencies(self, frequencies, max_font_size)
   506                     font, orientation=orientation)
   507                 # get size of resulting text
--> 508                 box_size = draw.textbbox((0, 0), word, font=transposed_font, anchor="lt")
   509                 # find possible places using integral image:
   510                 result = occupancy.sample_position(box_size[3] + self.margin,

~\anaconda3\lib\site-packages\PIL\ImageDraw.py in textbbox(self, xy, text, font, anchor, spacing, align, direction, features, language, stroke_width, embedded_color)
   565             font = self.getfont()
   566         mode = "RGBA" if embedded_color else self.fontmode
--> 567         bbox = font.getbbox(
   568             text, mode, direction, features, language, stroke_width, anchor
   569         )

AttributeError: 'TransposedFont' object has no attribute 'getbbox'

This is the full error trace I got when I run the code in my jupyter notebook.

What is this error and how to resolve this? I can't understand what is that TransposedFont when I haven't used any argument of that name in the WordCloud function.

1

There are 1 answers

0
Wayne On BEST ANSWER

The solution was to upgrade pip and pillow, as based on an issue posted at GitHub.
See the comments below the post above for how it solved the error featured here.