生成中文词云,直接上代码
def create_wordcloud(text_path, image_path):
#需要对中文进行处理
import jieba
#词云
from PIL import Image
import numpy as np
#词云生成工具
from wordcloud import WordCloud
try:
#背景图
bg=np.array(Image.open("C:\\Users\\74062\\Desktop\\szll.png"))
f = open(text_path,encoding="utf8")
raw_text = f.read()
seg_list = jieba.cut(raw_text, cut_all=False)
text = ' '.join(seg_list)
#生成
wc=WordCloud(
background_color="white",
max_words=150,
mask=bg, #设置图片的背景
max_font_size=60,
random_state=42,
font_path='C:/Windows/Fonts/Deng.ttf' #中文处理,用系统自带的字体,等线常规,如果有错误的话就改
).generate(text)
#保存云图
wc.to_file(image_path)
return "运行成功!"
except:
return "运行失败!请查看路径是否正确"