Error message 'AttributeError: module 'PIL.Image' has no attribute 'Transpose'' when generating a word cloud

Viewed 812

I am trying to generate a word cloud from a pandas column.

#text I want to create word cloud with

text = " ".join(review for review in df.Column.astype(str))

That runs fine. However when I run:

wordcloud = WordCloud(stopwords=stopwords).generate(text)

I receive the following error:

AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_24400/883004456.py in <module>
----> 1 wordcloud = WordCloud(stopwords=stopwords).generate(text)

~\.conda\envs\jupyterlab\lib\site-packages\wordcloud\wordcloud.py in generate(self, text)
    630         self
    631         """
--> 632         return self.generate_from_text(text)
    633 
    634     def _check_generated(self):

~\.conda\envs\jupyterlab\lib\site-packages\wordcloud\wordcloud.py in generate_from_text(self, text)
    612         """
    613         words = self.process_text(text)
--> 614         self.generate_from_frequencies(words)
    615         return self
    616 

~\.conda\envs\jupyterlab\lib\site-packages\wordcloud\wordcloud.py in generate_from_frequencies(self, frequencies, max_font_size)
    444                 font_size = self.height
    445             else:
--> 446                 self.generate_from_frequencies(dict(frequencies[:2]),
    447                                                max_font_size=self.height)
    448                 # find font sizes

~\.conda\envs\jupyterlab\lib\site-packages\wordcloud\wordcloud.py in generate_from_frequencies(self, frequencies, max_font_size)
    499                     font, orientation=orientation)
    500                 # get size of resulting text
--> 501                 box_size = draw.textsize(word, font=transposed_font)
    502                 # find possible places using integral image:
    503                 result = occupancy.sample_position(box_size[1] + self.margin,

~\.conda\envs\jupyterlab\lib\site-packages\PIL\ImageDraw.py in textsize(self, text, font, spacing, direction, features, language, stroke_width)
    581         if font is None:
    582             font = self.getfont()
--> 583         return font.getsize(text, direction, features, language, stroke_width)
    584 
    585     def multiline_textsize(

~\.conda\envs\jupyterlab\lib\site-packages\PIL\ImageFont.py in getsize(self, text, *args, **kwargs)
    794     def getsize(self, text, *args, **kwargs):
    795         w, h = self.font.getsize(text)
--> 796         if self.orientation in (Image.Transpose.ROTATE_90, Image.Transpose.ROTATE_270):
    797             return h, w
    798         return w, h

~\.conda\envs\jupyterlab\lib\site-packages\PIL\Image.py in __getattr__(name)
     63             stacklevel=2,
     64         )
---> 65         return categories[name]
     66     elif name in ("NEAREST", "NONE"):
     67         warnings.warn(

AttributeError: module 'PIL.Image' has no attribute 'Transpose'```
1 Answers

Reason

The worldcloud module doesn't specify the version of Pillow. In consideration of compatibility with worldcloud, you'd better stick with Pillow 8.4.0.

PIL.Image.Transpose is not available until Pillow 9.1.0.

Caveat

According to the traceback, it seems the Pillow installation on your machine is broken:

  • PIL\ImageFont.py indicated usage of Image.Transpose.ROTATE_90, which means this part of Pillow is from after 9.1.0
  • while PIL\Image.py raised exc Image.Transpose, indicating this part of source file is from Pillow < 9.1.0

Please, please, please. Don't mix use pip install and conda install. Remove your conda environment jupyterlab and re-create it. But this time, only install Python packages with one tool: pip or conda, not both of them.

Extended Reading

Related