import jieba import wordcloud import imageio from wordcloud import ImageColorGenerator #读取本地图片 img = imageio.imread('img.png') #读取保存的弹幕数据 data = open('3.txt',encoding='utf-8') text = data.read() #print(text) #分割词语 word = jieba.lcut(text) print(word) #转换为字符串 str = ' '.join(word) print(str) # 生成颜色映射 image_colors = ImageColorGenerator(img) #绘制词云图 wc = wordcloud.WordCloud( width=800, height=800, background_color='white', mask=img, contour_color='black', contour_width=1, stopwords={'哈','了','的','啊','你','是','我','吗','这','他','都','吧','这个','和','也','在','好','有','不','就','哈哈哈','不是','就是','说', '真的','很','人','看','还','哈哈','哈哈哈哈','还是','没有','没','呢','什么','要','能','给'}, font_path='msyh.ttc' ) wc.generate(str) wc.recolor(color_func=image_colors) wc.to_file("ai词云.png")