
贴上源码
import numpy as np;
from PIL import Image
import re
import jieba
from wordcloud import WordCloud,ImageColorGenerator,STOPWORDS
#读取文件
with open('playfull power.txt',"r",encoding='utf-8') as f:
word = (f.read());
f.close();
#词云形状 字体
image = np.array(Image.open('1002.jpg'));
font = 'msyh.ttc';
#去掉英文
resultword = re.sub("[A-Za-z0-9\[\`\~\!\@\#\$\^\&\*\(\)\=\|\{\}\'\:\;\'\,\[\]\.\<\>\/\?\~\。\@\#\\\&\*\%]", "",word);
wordlist_from_jieba = jieba.cut(resultword)
wl_generate = " ".join(wordlist_from_jieba);
stop_words = set(STOPWORDS)
stop_words.add("如果");
stop_words.add("资料");
stop_words.add("搜索");
stop_words.add("一个");
stop_words.add("可以");
stop_words.add("可能");
stop_words.add("因为");
stop_words.add("或者");
stop_words.add("需要");
stop_words.add("但是");
stop_words.add("没有");
stop_words.add("一些");
stop_words.add("这些");
stop_words.add("不是");
stop_words.add("那么");
stop_words.add("这个");
stop_words.add("这样");
stop_words.add("什么");
my_wordcloud = WordCloud(scale=4,font_path=font,mask=image,stopwords=stop_words,background_color='white'
,max_words=100).generate(wl_generate);
to_img = my_wordcloud.to_image();
to_img.show();
my_wordcloud.to_file("playfull power.jpg");
网友评论