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.

34 lines
1.0 KiB

2 months ago
import pandas as pd
from wordcloud import WordCloud, ImageColorGenerator
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
# 文件路径
input_file = 'danmu.txt' # 输入文件名
# 从文本文件中读取弹幕数据
with open(input_file, 'r', encoding='utf-8') as file:
danmaku_text = file.read()
# 创建词云对象
wordcloud = WordCloud(
width=800,
height=600,
background_color="white",
max_words=200,
colormap='viridis', # 颜色主题
random_state=42,
font_path='C:\Windows\Fonts\STZHONGS.TTF' # 字体路径,如果需要支持中文显示
).generate(danmaku_text)
# 可选:使用自定义图片作为背景
mask = np.array(Image.open('image.jpg')) # 替换为你的图片文件名
image_colors = ImageColorGenerator(mask)
wordcloud.recolor(color_func=image_colors)
plt.figure(figsize=(10, 8))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off") # 不显示坐标轴
plt.title('弹幕词云图')
plt.show()