使用jieba和wordcloud生成图形词云
pip install -r requirements.txt
简单显示
from words_image import get_keywords, show_image
import os
BASE_DIR = os.path.dirname(__file__)
# 文本文件夹路径
DATA_DIR = os.path.join(BASE_DIR, "data")
# 图片文件夹路径
IMAGE_DIR = os.path.join(BASE_DIR, "images")
# 源图片路径
# 其中图片中白色的部分会被忽略
IMAGE_PATH = os.path.join(IMAGE_DIR, "tony.png")
if __name__ == "__main__":
words = get_keywords(DATA_DIR)
# 显示生成的词图
show_image(words, IMAGE_PATH)
批量处理并保存
from words_image import get_keywords, save_image
import os
BASE_DIR = os.path.dirname(__file__)
# 文本文件夹路径
DATA_DIR = os.path.join(BASE_DIR, "data")
# 图片文件夹路径
IMAGE_DIR = os.path.join(BASE_DIR, "images")
# 结果保存路径
OUTPUT_DIR = os.path.join(BASE_DIR, "output")
if __name__ == "__main__":
words = get_keywords(DATA_DIR)
for image in os.listdir(IMAGE_DIR):
image_name = os.path.join(IMAGE_DIR, image)
save_image(words, image_name, OUTPUT_DIR)
路飞
乔巴