|
|
|
@ -4,22 +4,22 @@ from wordcloud import WordCloud
|
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
from PIL import Image
|
|
|
|
|
import numpy as np
|
|
|
|
|
import imageio
|
|
|
|
|
import imageio.v2 as imageio
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
#读取停用词表
|
|
|
|
|
def load_stopwords(file_path):
|
|
|
|
|
#加载停用词表。
|
|
|
|
|
with open(file_path, 'r', encoding='utf-8') as f:
|
|
|
|
|
stopwords = set(line.strip() for line in f)
|
|
|
|
|
return stopwords
|
|
|
|
|
|
|
|
|
|
#过滤停用词
|
|
|
|
|
def remove_stopwords(words_list, stopwords):
|
|
|
|
|
# 从词汇列表中移除停用词。
|
|
|
|
|
return [word for word in words_list if word not in stopwords and len(word) > 1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generate_(file_path):
|
|
|
|
|
# 生成包含弹幕数据的词云 GIF 文件。
|
|
|
|
|
|
|
|
|
|
# 读取 Excel 文件并提取弹幕内容
|
|
|
|
|
df = pd.read_excel(file_path)
|
|
|
|
|
comments = df['danmu'].astype(str)
|
|
|
|
@ -39,11 +39,12 @@ def generate_(file_path):
|
|
|
|
|
words_list = ' '.join(filtered_words)
|
|
|
|
|
|
|
|
|
|
# 加载形状图片并生成词云
|
|
|
|
|
mask = np.array(Image.open("D://edge//kk.png"))
|
|
|
|
|
mask = np.array(Image.open("D://edge//kk.png")) # 替换为实际路径
|
|
|
|
|
|
|
|
|
|
# 创建一个用于存储帧的文件夹
|
|
|
|
|
if not os.path.exists("wordcloud_frames"):
|
|
|
|
|
os.makedirs("wordcloud_frames")
|
|
|
|
|
frames_dir = "wordcloud_frames"
|
|
|
|
|
if not os.path.exists(frames_dir):
|
|
|
|
|
os.makedirs(frames_dir)
|
|
|
|
|
|
|
|
|
|
# 生成多个帧
|
|
|
|
|
filenames = []
|
|
|
|
@ -57,24 +58,29 @@ def generate_(file_path):
|
|
|
|
|
contour_color='black',
|
|
|
|
|
width=800,
|
|
|
|
|
height=600,
|
|
|
|
|
color_func=lambda *args, **kwargs: (np.random.randint(50, 255), np.random.randint(50, 255), np.random.randint(50, 255)), # 随机颜色
|
|
|
|
|
color_func=lambda *args, **kwargs: (
|
|
|
|
|
np.random.randint(50, 255),
|
|
|
|
|
np.random.randint(50, 255),
|
|
|
|
|
np.random.randint(50, 255)
|
|
|
|
|
), # 随机颜色
|
|
|
|
|
random_state=i # 每帧的随机状态不同
|
|
|
|
|
).generate(words_list)
|
|
|
|
|
|
|
|
|
|
# 保存每一帧为图片
|
|
|
|
|
filename = f"wordcloud_frames/frame_{i}.png"
|
|
|
|
|
filename = os.path.join(frames_dir, f"frame_{i}.png")
|
|
|
|
|
wordcloud.to_file(filename)
|
|
|
|
|
filenames.append(filename)
|
|
|
|
|
|
|
|
|
|
# 将所有帧合成为 GIF
|
|
|
|
|
with imageio.get_writer("wordcloud_animation.gif", mode="I", duration=0.5) as writer: # duration表示每帧的显示时间
|
|
|
|
|
gif_path = "wordcloud.gif"
|
|
|
|
|
with imageio.get_writer(gif_path, mode="I", duration=0.5) as writer: # duration 表示每帧的显示时间
|
|
|
|
|
for filename in filenames:
|
|
|
|
|
image = imageio.imread(filename)
|
|
|
|
|
image = imageio.imread(filename) # 读取每一个图片
|
|
|
|
|
writer.append_data(image)
|
|
|
|
|
|
|
|
|
|
# 清理临时帧文件
|
|
|
|
|
for filename in filenames:
|
|
|
|
|
os.remove(filename)
|
|
|
|
|
|
|
|
|
|
print("词云动图生成成功,保存为 'wordcloud_animation.gif'")
|
|
|
|
|
print(f"GIF created at {gif_path}")
|
|
|
|
|
return 0
|
|
|
|
|