# coding=utf-8
from wordcloud import WordCloud
import jieba
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties from scipy.misc import imread
def read_file():
with open('comment.txt', 'r', encoding='utf-8') as f:
text = f.read()
return text
def data_img():
# 结巴分词
wordlist = jieba.cut(read_file(), cut_all=True)
wl = ' '.join(wordlist)
# print(wl)
# 词云
tb_pic = imread('00.png') # 返回的是 numpy.ndarray 也即 numpy 下的多维数组对象; wc = WordCloud(
# 设置颜色
background_color='white',
# 设置最大显示词云数
max_words=500,
mask=tb_pic, # 设置背景图片
# 设置下字体
font_path='C:\windows\fonts\simsun.ttc',
height=1200,
width=1600,
# 字体最大值
max_font_size=100,
# 设置有多少种随机生成状态,即有多少种配色方案
random_state=50,
)
myword = wc.generate(wl)
print(myword)
# 图片展示 词云
plt.figure(figsize=(16, 9)) # 图片大小
plt.imshow(myword) # 函数负责对图像进行处理,并显示其格式, 不会显示图片
plt.axis('off') # 不显示坐标尺寸
# plt.legend(prop=font1)
plt.show()
# wc.to_file('comment.jpg')
if __name__ == '__main__':
# 评论内容生成词云
data_img()