From 8976767fd0966a3c48aea6d868986663720cc9b4 Mon Sep 17 00:00:00 2001 From: ptlwmhjs9 <2869950691@qq.com> Date: Wed, 18 Sep 2024 19:22:29 +0800 Subject: [PATCH] ADD file via upload --- 词云图制作.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 词云图制作.py diff --git a/词云图制作.py b/词云图制作.py new file mode 100644 index 0000000..f5504cf --- /dev/null +++ b/词云图制作.py @@ -0,0 +1,34 @@ +from wordcloud import WordCloud +from PIL import Image +import matplotlib.pyplot as plt +import numpy as np + +# 定义文本文件路径 +text_file_path = 'MID、BVID、CID及弹幕.txt' +# 定义遮罩图片路径(确保是具有Alpha通道的PNG图片) +mask_image_path = 'MASK.png' + +# 读取文本文件 +with open(text_file_path, 'r', encoding='utf-8') as file: + txt = file.read() + +# 加载遮罩图片 +mask = np.array(Image.open(mask_image_path)) + +# 注意:WordCloud 可以直接处理具有Alpha通道的PNG图片,所以这里不需要额外处理 + +# 创建WordCloud对象,并设置mask +wordcloud = WordCloud( + background_color="white", + width=800, + height=600, + max_words=200, + max_font_size=80, + mask=mask, + contour_width=3, + contour_color='steelblue', + font_path="msyh.otf" # 确保字体文件路径正确 +).generate(txt) + +# 保存词云为图片文件 +wordcloud.to_file('词云图.png') \ No newline at end of file