图片

import jieba
import wordcloud
# imread用来设置背景图片
from imageio import imread
r1 = open("校长2018.txt", "r", encoding="utf-8")
t1 = r1.read()
r1.close()
# 利用jieba库中的lcut函数实现对t1的切割分类
ls1 = jieba.lcut(t1)
txt1 = " ".join(ls1)
# 设置长宽高背景颜色,字体等 这里设置字体必须小写.
w1 = wordcloud.WordCloud(width=1000, height=800, background_color="black", font_path="msyh.ttc")
w1.generate(txt1)
w1.to_file('cp1.png')

# 自定义背景颜色和背景图片
background_color = str(input("输入你想要的背景颜色:"))
background_picture = str(input("输入你想要的背景的绝对路径:"))
mk = imread(background_picture)
r2 = open("校长2018毕业讲话.txt", "r", encoding="utf-8")
t2 = r2.read()
r2.close()
ls2 = jieba.lcut(t2)
txt2 = " ".join(ls2)
w2 = wordcloud.WordCloud(width=1000, height=800, background_color=background_color, font_path="simhei.ttf", mask=mk)
w2.generate(txt2)
w2.to_file('cp2.png')
# D:/Pycharm/xiaoyu.jpg

上一篇:【Unity】1.3 Unity3D游戏开发学习路线


下一篇:词云