'ImageDraw'对象没有属性'textbbox'

问题描述 投票:0回答:3

我正在开发一个简单的文本挖掘项目。当我尝试创建词云时,出现以下错误:

AttributeError: 'ImageDraw' object has no attribute 'textbbox'

我有一个新闻数据集及其类别;为了创建词云,我尝试预处理文本:


import pandas as pd
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from textblob import Word
from wordcloud import WordCloud 

newsData = pd.read_csv("data.txt", sep= '\t', header=None, 
                       names=["Description", "Category", "Tags"],on_bad_lines='skip', 
                       engine='python' , encoding='utf-8')
#print(newsData.head())

newsData['Description'] =  newsData['Description'].apply(lambda x:  " ".join(x.lower() for x in x.split()))
newsData['Category'] =  newsData['Category'].apply(lambda x:  " ".join(x.lower() for x in x.split()))
newsData['Tags'] =  newsData['Tags'].apply(lambda x:  " ".join(x.lower() for x in x.split()))

# stopword filtering
stop = stopwords.words('english')
newsData['Description'] =  newsData['Description'].apply(lambda x: " ".join (x for x in x.split() if x not in stop))
#stemming

st = PorterStemmer()
newsData['Description'] =  newsData['Description'].apply(lambda x: " ".join ([st.stem(word) for word in x.split()]))
newsData['Category'] =  newsData['Category'].apply(lambda x: " ".join ([st.stem(word) for word in x.split()]))
newsData['Tags'] =  newsData['Tags'].apply(lambda x: " ".join ([st.stem(word) for word in x.split()]))

#lemmatize

newsData['Description'] =  newsData['Description'].apply(lambda x: " ".join ([Word(word).lemmatize() for word in x.split()]))
newsData['Category'] =  newsData['Category'].apply(lambda x: " ".join ([Word(word).lemmatize() for word in x.split()]))
newsData['Tags'] =  newsData['Tags'].apply(lambda x: " ".join ([Word(word).lemmatize() for word in x.split()]))
#print(newsData.head())


culture = newsData[newsData['Category'] == 'culture'].sample(n=200)
health = newsData[newsData['Category'] == 'health'].sample(n=200)
dataSample = pd.concat([culture, health],axis=0)

culturesmpl = culture[culture['Category'] == 'culture'].sample(n=200)
healthspml = health[health['Category'] == 'health'].sample(n=200)
#print(dataSample.head())

cultureSTR = culturesmpl.Description.str.cat()
healthSTR = healthspml.Description.str.cat()
#print(spam_str)

然后我尝试使用 WordCloud 库创建词云

wordcloud_culture =  WordCloud(collocations= False, background_color='white' ).generate(cultureSTR)

# Plot
plt.imshow(wordcloud_culture, interpolation='bilinear')
plt.axis('off')
plt.show()

但是运行此代码后我收到错误:

  File ~/anaconda3/lib/python3.9/site-packages/wordcloud/wordcloud.py:508 in generate_from_frequencies
    box_size = draw.textbbox((0, 0), word, font=transposed_font, anchor="lt")

AttributeError: 'ImageDraw' object has no attribute 'textbbox'

你知道我该如何解决这个问题吗?

python pandas matplotlib plot word-cloud
3个回答
1
投票

textsize 函数在 Pillow 10 中已弃用。

因此安装 Pillow 9.5.0 将解决该问题:

pip3 install Pillow==9.5.0

0
投票

我安装的是pillow 7.1.1,然后我将其升级到9.5.0。然后错误就消失了!

因此,如果您遇到同样的问题,可以使用以下方法解决:

python3 -m pip install --upgrade Pillow

0
投票

历史

ImageDraw.textsize()
方法在PIL版本9.2.0中已弃用,并从2023年7月1日版本10.0.0开始完全删除

ImageDraw.textbbox()
方法在8.0.0版本中引入,作为更强大的解决方案。

示例

如果您只是想替换一行代码,而您之前已经这样做过

text_width, text_height = ImageDraw.Draw(image).textsize(your_text, font=your_font)

..那么你可以使用

_, _, text_width, text_height = ImageDraw.Draw(image).textbbox((0, 0), your_text, font=your_font)

说明

textsize()
将文本的nominal 宽度和高度的尺寸输出为元组:
(width, height)
textbbox()
将边界框的 x 和 y 范围输出为元组:
(left, top, right, bottom)

_, _,
开始该行是丢弃输出元组的前两个元素的一种方法。

添加

(0, 0)
作为
textbbox()
中的第一个参数,告诉它将边界框锚定在原点。

避免依赖过时的库,并探索这种变化的原因以及为什么

textbbox()
是一种更稳健的方法!

© www.soinside.com 2019 - 2024. All rights reserved.