# coding=utf-8
# Author:XiaoBing
# Date:2021/3/29 21:19
import jieba #分词
from matplotlib import pyplot as plt #绘图,数据可视化
from wordcloud import WordCloud #词云
from PIL import Image #图片处理
import numpy as np #矩阵运算
import sqlite3 #数据库
class word_count:
def __init__(self,file_path,img_path,final_imgpath):
self.file_path = file_path
self.img_path = img_path
self.final_imgpath = final_imgpath
# 准备词云所需要的文字
def word_cloud(self):
with open(self.file_path,mode='r',encoding='utf-8') as f:
text = ''
data = f.readlines()
for item in data:
text += item
# print(text)
# con = sqlite3.connect('movie.db')
# cur = con.cursor()
# sql = 'select instroduction from movie250'
# data = cur.execute(sql)
# text = ''
# for item in data:
# text = text + item[0]
# print(text)
# cur.close()
# con.close()
# 分词
cut = jieba.cut(text)
string = ' '.join(cut)
# 图片处理
img = Image.open(self.img_path)
img_array = np.array(img) #将图片转换为数组
# 按照什么格式规则呈现--封装一个对象
wc = WordCloud(
background_color= 'white',
mask = img_array,
font_path = 'msyh.ttc'#字体位置在:c\windows\fonts
)
wc.generate_from_text(string)
# 绘制图片
fig = plt.figure(1)
plt.imshow(wc)
plt.axis('off') #是否显示坐标轴
#plt.show() #显示生成的词云图片
# 输出词云图片
plt.savefig(self.final_path,dpi = 300) #保存路径+像素
if __name__ == '__main__':
wc = word_count(
file_path=r'C:\Users\小冰\Desktop\十四五规划.txt',
img_path=r'C:\Users\小冰\Desktop\tree.jpg',
final_imgpath=r'C:\Users\小冰\Desktop\tree1.jpg'
)
wc.word_cloud()
从这里到这里~~~~~~