pip install jieba
pip install wordcloud(如果安装失败,https://www.lfd.uci.edu/~gohlke/pythonlibs/#wordcloud 根据python地址下载,安装命令为:pip install xxxx)
# -*- coding: utf-8 -*-
import jieba
from matplotlib import pyplot as plt
# from wordcloud import WordCloud
from wordcloud import WordCloud,STOPWORDS,ImageColorGenerator
from PIL import Image
import numpy as np
text = ''
font = r'111.ttc'
#读取文字文件
with open('sentence_good_morning.txt','r',encoding='utf-8', errors='ignore') as fin:
for line in fin.readlines():
line = line.strip('
');
text += ' '.join(jieba.cut(line));
text += ' ';
img = Image.open('yy.png') #打开图片
img_array = np.array(img) #将图片装换为数组
wc = WordCloud(background_color='white',
max_words=900,
stopwords=STOPWORDS,
font_path='111.ttc', # 设置字体格式
max_font_size=700, # 设置字体最大值
random_state=100, # 设置有多少种随机生成状态,即有多少种配色方案
width=1000, #生成图片的大小
height=800,
mask=img_array
);
wc.generate_from_text(text);
plt.imshow(wc);
plt.axis('off');
plt.figure();
plt.show();
wc.to_file("yy.jpg");