1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| import jieba from PIL import Image import numpy as np from wordcloud import WordCloud, ImageColorGenerator import matplotlib.pyplot as plt
txt_path='聊天文件地址如./11.txt' txt_open=(open(txt_path,encoding='UTF-8').read())
tran_pic = '如./12.jpg'
font='字体地址 如 ./STFangSong.ttf'
image = Image.open(tran_pic) image_= np.array(image)
world_jieba_after = jieba.cut(txt_open, cut_all=True) world_merge= ' '.join(world_jieba_after)
wc = WordCloud( background_color="white", max_words=2000, mask=image_, max_font_size=700,
collocations=False, margin=2, width=768, height=1024, font_path=font
)
wc.generate(world_merge)
image_colors = ImageColorGenerator(image_)
plt.figure() plt.imshow(wc) plt.axis('off') plt.show()
wc.to_file('2.png')
|