python script to generate WordCloud

Refer to the python API documentation: http://amueller.github.io/word_cloud/index.html
1. Generate a word cloud image using a cartoon character image.
Material picture
insert image description here
============ The picture material and text material in the example are in the same directory as the code ===============

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. Generate a word cloud in Chinese

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")

insert image description here

Guess you like

Origin blog.csdn.net/qq_40267002/article/details/111043386