赞
踩
- #导入必要的库
- import wordcloud as wc
- import random
- import jieba
- from PIL import Image
- import numpy as np
-
-
- #打开文本文件text.txt
- with open("text.txt",mode="r",encoding="utf-8") as fp:
- content = fp.read()
- res = jieba.lcut(content)
- text = " ".join(res)
-
- img=Image.open('PIC.png')
- graph = np.array(img)
-
- #画图
- word_cloud = wc.WordCloud(font_path="C:\Windows\Fonts\msyh.ttc",background_color="white")#字体、背景形状
- word_cloud.generate(text)
- word_cloud.to_file("方形图.png")#绘制到一个图片里
- Image.open("方形图.png")
-
- word_cloud = wc.WordCloud(font_path="C:\Windows\Fonts\msyh.ttc",background_color="white",mask=graph)#字体、背景形状
- word_cloud.generate(text)
- word_cloud.to_file("自定义图.png")#绘制到一个图片里
- Image.open("自定义图.png")

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。