在wordcloud.WordCloud中避免AttributeError: 'TransposedFont' object has no attribute 'getbbox'解决方法问题
时间: 2023-06-22 21:21:10 浏览: 1716
在使用WordCloud库生成词云图时,如果要避免出现`AttributeError: 'TransposedFont' object has no attribute 'getbbox'`的错误,可以使用以下两种方法:
1. 升级Pillow库至最新版本。这个问题在Pillow 7.0.0版本中已经被修复。可以使用以下命令升级Pillow库:
```
pip install --upgrade Pillow
```
2. 修改WordCloud库中的源代码。打开WordCloud库的`wordcloud.py`文件,找到`fit_words(self, words)`方法,将其中的
```python
font = ImageFont.truetype(self.font_path, font_size)
transposed_font = ImageFont.TransposedFont(font, orientation=self.layout_)
```
替换为
```python
font = ImageFont.truetype(self.font_path, font_size)
if self.layout_ == 3:
transposed_font = font
else:
transposed_font = ImageFont.TransposedFont(font, orientation=self.layout_)
```
这样就可以避免使用TransposedFont对象时出现getbbox()方法不存在的错误了。注意,修改源代码可能会影响到其他使用WordCloud库的代码,建议在备份源代码后再进行修改。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![png](https://img-home.csdnimg.cn/images/20210720083516.png)