python脚本生成WordCloud

参考python API文档:http://amueller.github.io/word_cloud/index.html
1、使用卡通人物图片生成一个词云图片。
素材图片
在这里插入图片描述
=========== 实例中图片素材和文字素材与代码在同一目录下 ==============

msg = open(file="ooo.txt", mode="r", encoding="utf-8").read()
bg_pic = imread("0.png")

wordcloud = WordCloud(
    mask=bg_pic,
    background_color="white",
    scale=1.5,
    width=30,
    height=30,
    max_words=1000).generate(msg)
imsge_color = ImageColorGenerator(bg_pic)
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
wordcloud.to_file("qq.jpg")

2、使用中文生成一个词云

with open(file="msg.txt", mode="r", encoding="utf-8")as qq:
    txt = qq.read()
    sun = wordcloud.WordCloud(
        width=1000,
        font_path=r"C:\Windows\Fonts\STXINGKA.TTF",
        height=700)
    sun.generate(" ".join(jieba.lcut(txt)))
    sun.to_file(filename="chinese.jpg")

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_40267002/article/details/111043386