Python用来做图像词云图片

根据上一篇的词云:https://blog.csdn.net/qq_41709494/article/details/89213176

1.在cmd安装库四个库 

pip install jieba
 
pip install  matplotlib
 
pip  install  wordcloud

pip  install  imageio

2.代码如下

# coding=UTF-8
import jieba
from imageio import imread
from wordcloud import WordCloud

mask = imread('E:\\python项目\\aili.jpg')
with open("E:\\python项目\\标准输出与输入.txt") as fp:    #as后面是别名
    txt = fp.read()

words = jieba.lcut(txt)  # 精确分词

nextword = ' '.join(words)  # 空格连接字符

wordshow = WordCloud(background_color='white',
                     width=800,
                     height=600,
                     max_words=200,
                     max_font_size=80,
                     font_path="msyh.ttc",    #用微软雅黑作为字体显示效果
                     mask = mask,             #转为词云形状
                      ).generate(nextword)

wordshow.to_file('tes4.png')  # 转换成图片

3.准备好的图片

 

4.显示效果 

猜你喜欢

转载自blog.csdn.net/qq_41709494/article/details/93345236