You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.5 KiB

import jieba
from matplotlib import pyplot as plt
from wordcloud import WordCloud
from PIL import Image
import numpy as np
def predeal(txt):
cut = jieba.cut(txt) # 分词
string = ' '.join(cut)
return string
def drawcloud(imgpath, stopword, string, savepath):
try:
img = Image.open(imgpath) # 打开图片
except IOError as e:
print(f"打开图片时出错:{e}")
return
img_array = np.array(img) # 将图片转换为数组
wordcloud = WordCloud(
background_color='white',
width=1000,
height=800,
mask=img_array, # 设置背景图片
font_path='C:\\Windows\\Fonts\\msyh.ttc', # 电脑自带的字体
stopwords=stopword,
colormap='plasma'
)
wordcloud.generate_from_text(string) # 绘制图片
plt.imshow(wordcloud)
plt.axis('off')
wordcloud.to_file(savepath) # 保存图片
if __name__ == '__main__':
txtpath = r'd:\学习\软件工程\swork\res\select_ai.txt'
imgpath = r'd:\学习\软件工程\swork\tu4.png'
savepath = r'd:\学习\软件工程\swork\cloud_ai.png'
stopword = ['', '', '', '视频', '', '', '', '哈哈哈', '', '', '', '','福州大学'] # 不想显示的词
try:
with open(txtpath, 'r', encoding='utf-8') as file:
txt = file.read()
except FileNotFoundError as e:
print(f"读取文本文件时出错:{e}")
else:
string = predeal(txt)
drawcloud(imgpath, stopword, string, savepath)