|
|
@ -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')
|