Python 生成所需形状的wordcloud?

Python 生成所需形状的wordcloud?,python,matplotlib,word-cloud,Python,Matplotlib,Word Cloud,我试图用Python制作一个我想要的形状的wordcloud,方法是使用一个以单词计数为值的字典。为此,我从web上读取了一个图像。图像已正确存储在变量中,但未呈现wordcloud。但使用默认形状,wordcloud将显示。以下是我的代码: from PIL import Image import urllib import requests img=Image.open(requests.get('https://internationalcriminallaw.files.wordpres

我试图用Python制作一个我想要的形状的wordcloud,方法是使用一个以单词计数为值的字典。为此,我从web上读取了一个图像。图像已正确存储在变量中,但未呈现wordcloud。但使用默认形状,wordcloud将显示。以下是我的代码:

from PIL import Image
import urllib
import requests
img=Image.open(requests.get('https://internationalcriminallaw.files.wordpress.com/2013/04/terrorism.png', stream=True).raw)
motive=terror['Motive'].str.lower().str.replace(r'\|', ' ').str.cat(sep=' ')
words=nltk.tokenize.word_tokenize(motive)
word_dist = nltk.FreqDist(words)
stopwords = nltk.corpus.stopwords.words('english')
words_except_stop_dist = nltk.FreqDist(w for w in words if w not in stopwords) 
mask1=img
wordcloud = WordCloud(
                      stopwords=STOPWORDS,
                      background_color='white',
                      mask=mask1,
                      max_font_size=40
                     )
wordcloud.generate(" ".join(words_except_stop_dist))
plt.imshow(wordcloud)
plt.axis('off')
plt.show()
错误如下:

AttributeError Traceback (most recent call last)
<ipython-input-147-6aabda55c848> in <module>()
  6                           max_font_size=40
  7                          )
----> 8 wordcloud.generate(" ".join(words_except_stop_dist))
  9 plt.imshow(wordcloud)
 10 plt.axis('off')

/home/austin/anaconda3/lib/python3.5/site-packages/wordcloud/wordcloud.py in generate(self, text)
448         self
449         """
--> 450         return self.generate_from_text(text)
451 
452     def _check_generated(self):

/home/austin/anaconda3/lib/python3.5/site-packages/wordcloud/wordcloud.py in generate_from_text(self, text)
434         """
435         words = self.process_text(text)
--> 436         self.generate_from_frequencies(words)
437         return self
438 

    /home/austin/anaconda3/lib/python3.5/site-packages/wordcloud/wordcloud.py in generate_from_frequencies(self, frequencies)
281         if self.mask is not None:
282             mask = self.mask
--> 283             width = mask.shape[1]
284             height = mask.shape[0]
285             if mask.dtype.kind == 'f':

/home/austin/anaconda3/lib/python3.5/site-packages/PIL/Image.py in __getattr__(self, name)
625             new['version'] = 3
626             return new
--> 627         raise AttributeError(name)
628 
629     def __getstate__(self):

AttributeError: shape
AttributeError回溯(最近一次调用)
在()
6最大字体大小=40
7                          )
---->8 wordcloud.generate(“.”join(单词\u除了\u stop\u dist))
9 plt.imshow(wordcloud)
10 plt.轴(“关闭”)
/生成中的home/austin/anaconda3/lib/python3.5/site-packages/wordcloud/wordcloud.py(self,text)
448自我
449         """
-->450返回self.generate_from_text(text)
451
452 def检查生成(自):
/home/austin/anaconda3/lib/python3.5/site-packages/wordcloud/wordcloud.py在generate_from_text(self,text)中
434         """
435字=自处理文本(文本)
-->436.从_频率(单词)生成_
437回归自我
438
/home/austin/anaconda3/lib/python3.5/site-packages/wordcloud/wordcloud.py中的generate_from_frequencies(self,frequencies)
281如果self.mask不是None:
282掩码=self.mask
-->283宽度=遮罩形状[1]
284高度=遮罩形状[0]
285如果mask.dtype.kind='f':
/home/austin/anaconda3/lib/python3.5/site-packages/PIL/Image.py in\uuuu getattr\uuuu(self,name)
625新['version']=3
626返回新
-->627提高属性错误(名称)
628
629定义获取状态(自身):
属性错误:形状

我该怎么办?

完整错误消息是什么?@DavidG updated error…请检查我们需要问题的详细信息以提供帮助。完整错误消息是什么?@DavidG updated error…请检查我们需要问题的详细信息以提供帮助。