sairate 7df250638d chore: 添加项目基础结构和示例代码
- 创建 .idea 目录和相关配置文件,设置项目结构
- 添加多个课堂成果示例代码,涵盖不同主题和功能
- 创建和配置 .gitignore 文件,忽略特定文件和目录
2025-07-05 09:36:00 +08:00

38 lines
875 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 运行程序前,请务必前往本课包【课程说明】处,下载课程素材,完成配置
# 编程前,运行【安装第三方库.py】文件安装本课用到的第三方库
import jieba
import wordcloud
from PIL import Image
import numpy as np
# 加载背景图片
mask_image = np.array(Image.open("心.png"))
with open('春.txt', 'r', encoding='utf-8') as f:
text = f.read()
lst = jieba.lcut(text)
m = ' '.join(lst)
print('开始制作词云...')
w = wordcloud.WordCloud(
font_path = '大梁体繁简.TTF',
stopwords = ['',"","",''],
# 使用参数background_color设置图片背景
background_color = None,
mode = 'RGBA',
mask = mask_image
)
w.generate(m)
w.to_file('词云-春.png')
print('词云图片已生成,开始展示图片')
p = Image.open('词云-春.png')
p.show()
print('图片展示完成')