Update generate_gif.py

main
luoyonghuang 2 months ago
parent b76889d9f9
commit 7b9883b235

@ -4,50 +4,51 @@ from wordcloud import WordCloud
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from PIL import Image from PIL import Image
import numpy as np import numpy as np
import imageio import imageio.v2 as imageio
import os import os
#读取停用词表
def load_stopwords(file_path): def load_stopwords(file_path):
#加载停用词表。
with open(file_path, 'r', encoding='utf-8') as f: with open(file_path, 'r', encoding='utf-8') as f:
stopwords = set(line.strip() for line in f) stopwords = set(line.strip() for line in f)
return stopwords return stopwords
#过滤停用词
def remove_stopwords(words_list, stopwords): def remove_stopwords(words_list, stopwords):
# 从词汇列表中移除停用词。
return [word for word in words_list if word not in stopwords and len(word) > 1] return [word for word in words_list if word not in stopwords and len(word) > 1]
def generate_(file_path): def generate_(file_path):
#读取Excel文件并提取弹幕内容 # 生成包含弹幕数据的词云 GIF 文件。
# 读取 Excel 文件并提取弹幕内容
df = pd.read_excel(file_path) df = pd.read_excel(file_path)
comments = df['danmu'].astype(str) comments = df['danmu'].astype(str)
text = ' '.join(comments) text = ' '.join(comments)
#使用 jieba 分词 # 使用 jieba 分词
words = jieba.cut(text, cut_all=False) words = jieba.cut(text, cut_all=False)
#加载停用词表 # 加载停用词表
stopwords_file = "D://edge//stop.txt" # 替换为实际路径 stopwords_file = "D://edge//stop.txt" # 替换为实际路径
stopwords = load_stopwords(stopwords_file) stopwords = load_stopwords(stopwords_file)
#去除停用词 # 去除停用词
filtered_words = remove_stopwords(words, stopwords) filtered_words = remove_stopwords(words, stopwords)
#将过滤后的词汇重新拼接为一个字符串 # 将过滤后的词汇重新拼接为一个字符串
words_list = ' '.join(filtered_words) 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"): frames_dir = "wordcloud_frames"
os.makedirs("wordcloud_frames") if not os.path.exists(frames_dir):
os.makedirs(frames_dir)
#生成多个帧 # 生成多个帧
filenames = [] filenames = []
for i in range(10): # 生成10帧 for i in range(10): # 生成 10
# 动态改变词云的颜色和随机状态 # 动态改变词云的颜色和随机状态
wordcloud = WordCloud( wordcloud = WordCloud(
font_path='simhei.ttf', # 确保支持中文 font_path='simhei.ttf', # 确保支持中文
@ -57,24 +58,29 @@ def generate_(file_path):
contour_color='black', contour_color='black',
width=800, width=800,
height=600, 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 # 每帧的随机状态不同 random_state=i # 每帧的随机状态不同
).generate(words_list) ).generate(words_list)
# 保存每一帧为图片 # 保存每一帧为图片
filename = f"wordcloud_frames/frame_{i}.png" filename = os.path.join(frames_dir, f"frame_{i}.png")
wordcloud.to_file(filename) wordcloud.to_file(filename)
filenames.append(filename) filenames.append(filename)
#将所有帧合成为GIF # 将所有帧合成为 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: for filename in filenames:
image = imageio.imread(filename) image = imageio.imread(filename) # 读取每一个图片
writer.append_data(image) writer.append_data(image)
# 清理临时帧文件 # 清理临时帧文件
for filename in filenames: for filename in filenames:
os.remove(filename) os.remove(filename)
print("词云动图生成成功,保存为 'wordcloud_animation.gif'") print(f"GIF created at {gif_path}")
return 0 return 0

Loading…
Cancel
Save