准备
- 图片一张(用于指定形状)
- 一段文本(用于词云统计)
from PIL import Image
from wordcloud import WordCloud, ImageColorGenerator
import matplotlib.pyplot as plt
import numpy as np
import jieba
def GetWordCloud():
path_txt = 'C:/Users/Asus/Desktop/wd.txt'
path_img = "C:/Users/Asus/Desktop/logo1.png"
f = open(path_txt, 'r', encoding='UTF-8').read()
background_image = np.array(Image.open(path_img))
cut_text = " ".join(jieba.cut(f))
wordcloud = WordCloud(
font_path="C:/Windows/Fonts/msyhbd.ttc",
background_color="white",
mask=background_image).generate(cut_text)
image_colors = ImageColorGenerator(background_image)
plt.imshow(wordcloud.recolor(color_func=image_colors), interpolation="bilinear")
plt.axis("off")
plt.show()
if __name__ == '__main__':
GetWordCloud()
运行代码,大功告成!