import jieba
import imageio
from wordcloud import WordCloud,ImageColorGenerator
from matplotlib import pyplot as plt
#字体库
font=r"D:\Python_things\FontFamily\Muya.ttf"
#待解析文本库
text=""
text_path=r"D:\Python_things\require.txt"
with open(text_path,"r",encoding="utf-8") as f:
text=f.read()
#
#背景图片
img_path=r"D:\Python_things\bgim.png"
bg_image=imageio.imread(img_path)
#
# 提取标点符号库
words_path=r"D:\Python_things\StopWords\stopwords.txt"
stopWords=[]
with open(words_path,"r",encoding="utf-8") as f:
stopWords=f.read().split("\n")
#
show_words=[]#定义显示数组
waiting_str=jieba.cut(text)#待显示的字符串,加入/
for res in waiting_str:
if (res.strip() not in stopWords)and res.strip()!=""and len(res.strip())>1:
show_words.append(res)
wc = WordCloud(
background_color='white',
width=1000,
height=800,
max_words=3000,
mask=bg_image,
font_path=font,
random_state=200,
min_font_size=15,
max_font_size=50
)
wc.generate(" ".join(show_words))
image_colors = ImageColorGenerator(bg_image)
plt.imshow(wc.recolor(color_func=image_colors))
plt.imshow(wc)
plt.axis("off")
plt.show()
wc.to_file("D:\Python_things\img.png")
运行结果: