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

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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