1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import matplotlib.pyplot as plt 4 from wordcloud import WordCloud,STOPWORDS,ImageColorGenerator 5 import jieba 6 from scipy.misc import imread 7 from os import path 8 9 def word_clould(file_path,stopwords_path,font_path,save_pic_name):10 text_from_file_with_apath = open(file_path,encoding='utf-8').read()11 12 wordlist_after_jieba = jieba.cut(text_from_file_with_apath)13 stop_words = [line.strip() for line in open(stopwords_path,encoding='utf-8')]14 wordlist_jieba=[word for word in wordlist_after_jieba if word not in stop_words]15 wl_space_split = " ".join(wordlist_jieba)16 # 读取mask/color图片17 d = path.dirname(__file__)18 #nana_coloring = imread(path.join(d,bgpicture_path))19 20 my_wordcloud = WordCloud(#background_color="white",21 width=1200,height=800,22 # max_font_size=30,23 #max_words=5000,24 margin = 5,25 #mask = nana_coloring,26 random_state = 30,27 #stopwords = STOPWORDS,28 font_path=font_path).generate(wl_space_split)29 30 #image_colors = ImageColorGenerator(nana_coloring)31 32 # recolor wordcloud and show33 #my_wordcloud.recolor(color_func=image_colors)34 plt.imshow(my_wordcloud)35 plt.axis("off")36 plt.show()37 38 my_wordcloud.to_file(path.join(d, save_pic_name))39 40 41 file_path='小米6发布会.txt'42 stopwords_path='E:\\stopwords.txt'43 #bgpicture_path="E:\\wb.jpg"44 font_path='E:\\SIMHEI.TTF'45 save_pic_name="cloudimg1.png"46 word_clould(file_path,stopwords_path,font_path,save_pic_name)