‘ImageDraw’对象没有’textbbox’属性。

huangapple go评论94阅读模式
英文:

'ImageDraw' object has no attribute 'textbbox'

问题

I am working on a simple text mining project. When I tried to create a word-cloud I got this error:

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

I have a dataset of News and their categories; to create a word-cloud I tried to preprocessing the text:

  1. import pandas as pd
  2. import numpy as np
  3. import pandas as pd
  4. import matplotlib.pyplot as plt
  5. from nltk.corpus import stopwords
  6. from nltk.stem import PorterStemmer
  7. from textblob import Word
  8. from wordcloud import WordCloud
  9. newsData = pd.read_csv("data.txt", sep='\t', header=None,
  10. names=["Description", "Category", "Tags"],on_bad_lines='skip',
  11. engine='python' , encoding='utf-8')
  12. newsData['Description'] = newsData['Description'].apply(lambda x: " ".join(x.lower() for x in x.split()))
  13. newsData['Category'] = newsData['Category'].apply(lambda x: " ".join(x.lower() for x in x.split()))
  14. newsData['Tags'] = newsData['Tags'].apply(lambda x: " ".join(x.lower() for x in x.split()))
  15. # stopword filtering
  16. stop = stopwords.words('english')
  17. newsData['Description'] = newsData['Description'].apply(lambda x: " ".join (x for x in x.split() if x not in stop))
  18. #stemming
  19. st = PorterStemmer()
  20. newsData['Description'] = newsData['Description'].apply(lambda x: " ".join ([st.stem(word) for word in x.split()]))
  21. newsData['Category'] = newsData['Category'].apply(lambda x: " ".join ([st.stem(word) for word in x.split()]))
  22. newsData['Tags'] = newsData['Tags'].apply(lambda x: " ".join ([st.stem(word) for word in x.split()]))
  23. #lemmatize
  24. newsData['Description'] = newsData['Description'].apply(lambda x: " ".join ([Word(word).lemmatize() for word in x.split()]))
  25. newsData['Category'] = newsData['Category'].apply(lambda x: " ".join ([Word(word).lemmatize() for word in x.split()]))
  26. newsData['Tags'] = newsData['Tags'].apply(lambda x: " ".join ([Word(word).lemmatize() for word in x.split()]))
  27. culture = newsData[newsData['Category'] == 'culture'].sample(n=200)
  28. health = newsData[newsData['Category'] == 'health'].sample(n=200)
  29. dataSample = pd.concat([culture, health],axis=0)
  30. culturesmpl = culture[culture['Category'] == 'culture'].sample(n=200)
  31. healthspml = health[health['Category'] == 'health'].sample(n=200)
  32. cultureSTR = culturesmpl.Description.str.cat()
  33. healthSTR = healthspml.Description.str.cat()

and then I tried to create a wordcloud using WordCloud library

  1. wordcloud_culture = WordCloud(collocations= False, background_color='white' ).generate(cultureSTR)
  2. # Plot
  3. plt.imshow(wordcloud_culture, interpolation='bilinear')
  4. plt.axis('off')
  5. plt.show()

but after running this code I got the error:

  1. File ~/anaconda3/lib/python3.9/site-packages/wordcloud/wordcloud.py:508 in generate_from_frequencies
  2. box_size = draw.textbbox((0, 0), word, font=transposed_font, anchor="lt")
  3. AttributeError: 'ImageDraw' object has no attribute 'textbbox'

Do you know how can I fix this?

英文:

I am working on a simple text mining project. When I tried to create a word-cloud I got this error:

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

I have a dataset of News and their categories; to create a word-cloud I tried to preprocessing the text:

  1. import pandas as pd
  2. import numpy as np
  3. import pandas as pd
  4. import matplotlib.pyplot as plt
  5. from nltk.corpus import stopwords
  6. from nltk.stem import PorterStemmer
  7. from textblob import Word
  8. from wordcloud import WordCloud
  9. newsData = pd.read_csv("data.txt", sep= '\t', header=None,
  10. names=["Description", "Category", "Tags"],on_bad_lines='skip',
  11. engine='python' , encoding='utf-8')
  12. #print(newsData.head())
  13. newsData['Description'] = newsData['Description'].apply(lambda x: " ".join(x.lower() for x in x.split()))
  14. newsData['Category'] = newsData['Category'].apply(lambda x: " ".join(x.lower() for x in x.split()))
  15. newsData['Tags'] = newsData['Tags'].apply(lambda x: " ".join(x.lower() for x in x.split()))
  16. # stopword filtering
  17. stop = stopwords.words('english')
  18. newsData['Description'] = newsData['Description'].apply(lambda x: " ".join (x for x in x.split() if x not in stop))
  19. #stemming
  20. st = PorterStemmer()
  21. newsData['Description'] = newsData['Description'].apply(lambda x: " ".join ([st.stem(word) for word in x.split()]))
  22. newsData['Category'] = newsData['Category'].apply(lambda x: " ".join ([st.stem(word) for word in x.split()]))
  23. newsData['Tags'] = newsData['Tags'].apply(lambda x: " ".join ([st.stem(word) for word in x.split()]))
  24. #lemmatize
  25. newsData['Description'] = newsData['Description'].apply(lambda x: " ".join ([Word(word).lemmatize() for word in x.split()]))
  26. newsData['Category'] = newsData['Category'].apply(lambda x: " ".join ([Word(word).lemmatize() for word in x.split()]))
  27. newsData['Tags'] = newsData['Tags'].apply(lambda x: " ".join ([Word(word).lemmatize() for word in x.split()]))
  28. #print(newsData.head())
  29. culture = newsData[newsData['Category'] == 'culture'].sample(n=200)
  30. health = newsData[newsData['Category'] == 'health'].sample(n=200)
  31. dataSample = pd.concat([culture, health],axis=0)
  32. culturesmpl = culture[culture['Category'] == 'culture'].sample(n=200)
  33. healthspml = health[health['Category'] == 'health'].sample(n=200)
  34. #print(dataSample.head())
  35. cultureSTR = culturesmpl.Description.str.cat()
  36. healthSTR = healthspml.Description.str.cat()
  37. #print(spam_str)

and then I tried to create wordcloud using WordCloud library

  1. wordcloud_culture = WordCloud(collocations= False, background_color='white' ).generate(cultureSTR)
  2. # Plot
  3. plt.imshow(wordcloud_culture, interpolation='bilinear')
  4. plt.axis('off')
  5. plt.show()

but after running this code I got the error:

  1. File ~/anaconda3/lib/python3.9/site-packages/wordcloud/wordcloud.py:508 in generate_from_frequencies
  2. box_size = draw.textbbox((0, 0), word, font=transposed_font, anchor="lt")
  3. AttributeError: 'ImageDraw' object has no attribute 'textbbox'

do you know how can I fix this?

答案1

得分: 5

历史

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() 输出文本的名义宽度和高度作为一个元组:(宽度, 高度)
textbbox() 输出边界框的x和y范围作为一个元组:(左, 上, 右, 底)

_, _,开头是丢弃输出元组的前两个元素的一种方式。

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

避免依赖过时的库,探索这一变化的原因以及为什么textbbox()是一个更健壮的方法!

英文:

History

The ImageDraw.textsize() method was deprecated in PIL version 9.2.0 and completely removed beginning with version 10.0.0 on 2023-07-01.

The ImageDraw.textbbox() method was introduced in version 8.0.0 as a more robust solution.

Example

If you are looking to simply replace one line of code, and you previously had

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

..then you could instead use

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

Explanation

textsize() outputs dimensions for the nominal width and height of the text as a tuple: (width, height).
textbbox() outputs the x and y extents of the bounding box as a tuple: (left, top, right, bottom).

Starting the line with _, _, is a way to discard the first two elements of the output tuple.

Adding (0, 0) as the first argument in textbbox() tells it to anchor the bounding box at the origin.

Avoid relying on outdated libraries, and explore reasons for this change and why textbbox() is a more robust method!

答案2

得分: 2

"textsize"函数在Pillow 10中已被弃用。

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

  1. pip3 install Pillow==9.5.0
英文:

The textsize function is deprecated in Pillow 10.

Thus installing Pillow 9.5.0 will fix the issue:

  1. pip3 install Pillow==9.5.0

答案3

得分: 0

我安装了Pillow 7.1.1,然后升级到了9.5.0。然后错误消失了!

所以如果你面临相同的问题,可以使用以下命令修复:

  1. python3 -m pip install --upgrade Pillow
英文:

I was installed pillow 7.1.1, then I upgrade it to 9.5.0. and then the error was gone!

so if you face the same problem you can fix with:

  1. python3 -m pip install --upgrade Pillow

huangapple
  • 本文由 发表于 2023年5月7日 00:08:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定