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.

35 lines
1.3 KiB

import jieba
import wordcloud
import imageio
# 导入imageio库中的imread函数并用这个函数读取本地图片作为词云形状图片
def generate(file_path):
print("正在生成词云图...")
py = imageio.imread('.\\03.png') # 巴黎铁塔的蒙版图
# 读取文件内容
f = open(file_path, encoding='utf-8')
txt = f.read()
# jiabe 分词 分割词汇
txt_list = jieba.lcut(txt)
string = ' '.join(txt_list)
# 词云图设置
wc = wordcloud.WordCloud(
width=1000, # 图片的宽
height=700, # 图片的高
background_color='white', # 图片背景颜色
font_path='msyh.ttc', # 词云字体
mask=py, # 所使用的词云图片
scale=15,
# contour_color = 'steelblue' ,
contour_color='gold', # 设置轮廓为金色 (使用颜色名称)
contour_width=15, # 轮廓宽度
# 停用词
stopwords={'', '', '', '', '这是', '不是', '', '', '这个', '', ''}
)
# 给词云输入文字
wc.generate(string)
search_word = file_path.rsplit(".", 1)[0]
# 保存词云图
wc.to_file(f'{search_word}词云图.png')
print("成功保存词云图!")