ADD file via upload

main
ptlwmhjs9 2 months ago
parent 641b4d57ce
commit abbc2a9906

@ -0,0 +1,34 @@
from wordcloud import WordCloud
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
# 定义文本文件路径
text_file_path = 'AI弹幕·生成词云图用.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')
Loading…
Cancel
Save