美文网首页
Python词云图生成

Python词云图生成

作者: Then丶 | 来源:发表于2020-05-30 14:26 被阅读0次

首先安装库

pip install opencv-python
pip install numpy
pip install wordcloud
pip install jieba

import cv2
import wordcloud
import jieba 

mk = cv2.imread('N:\\codes\\Python\\generator\\20200530135116.png') #设置面具
w = wordcloud.WordCloud(width=1000,
                        height=700,
                        background_color='white',
                        mask=mk,
                        #font_path='msyh.ttc
                        font_path='C:/Windows/Fonts/simkai.ttf')

f = open('N:\\codes\\Python\\generator\\a3111.txt', encoding='utf-8') #提前保存的一个文章
txt = f.read()
txt_list = jieba.lcut(txt)
string = "".join(txt_list)
w.generate(string)
w.to_file('N:\\codes\\Python\\generator\\aaa.png') #生成图片
f.close()

#修改图片颜色
m_color = cv2.imread('N:\\codes\\Python\generator\\Color Hunt Palette 189889.png')
image_color = wordcloud.ImageColorGenerator(m_color)
wc_color = w.recolor(color_func=image_color)
wc_color.to_file('N:\\codes\\Python\\generator\\aaa.png')

#查看图片
img = cv2.imread("N:\\codes\\Python\\generator\\aaa.png")
cv2.imshow("", img)
cv2.waitKey(0) 
image.png

参考:
https://blog.csdn.net/zzc_zhuyu/article/details/90814902
https://blog.csdn.net/Yoci98/article/details/102837576
图片文字来源:
https://www.duanwenxue.com/article/649741.html

相关文章

网友评论

      本文标题:Python词云图生成

      本文链接:https://www.haomeiwen.com/subject/mqoczhtx.html