Python词云图生成

2020-05-30  本文已影响0人  Then丶

首先安装库

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

上一篇 下一篇

猜你喜欢

热点阅读