import numpy as np from PIL import Image # type: ignore import pandas as pd from wordcloud import WordCloud, ImageColorGenerator # type: ignore import matplotlib.pyplot as plt # type: ignore # 读取数据 df = pd.read_excel('ai_danmaku_result.xlsx') text = " ".join(df['弹幕内容']) # 加载背景图片 background_image = np.array(Image.open('background.jpg')) # 创建词云对象,设置背景图片和字体 wordcloud = WordCloud(width=800, height=400, background_color='white', mask=background_image, font_path='your_font.ttf').generate(text) # 使用背景图片的颜色作为词云的颜色 image_colors = ImageColorGenerator(background_image) plt.figure(figsize=(10, 5)) plt.imshow(wordcloud.recolor(color_func=image_colors), interpolation='bilinear') plt.axis('off') plt.show()