965-Python-Learning-Repo/Wordcloud.py
2023-05-25 19:47:27 +08:00

23 lines
641 B
Python

import jieba
import wordcloud
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
raw_data = open("./resources/111.txt", encoding="gbk").read()
ls = jieba.lcut(raw_data)
text = ' '.join(ls)
open("./resources/111.txt", encoding="gbk").close()
mask = np.array(Image.open("./resources/mask.png"))
wc = wordcloud.WordCloud(font_path="msyh.ttc",
mask=mask,
background_color='white',
max_font_size=240,
stopwords={'王勃', ''})
wc.generate(text)
wc.to_file("./results/111.png")
plt.imshow(wc)
plt.axis("off")
plt.show()