import jieba #分词
from matplotlib import pyplot as plt #绘图
from wordcloud import WordCloud #词云
from PIL import Image #图片处理
import numpy as np #矩阵运算
import sqlite3 #数据库
#准备词云所需要的文字(词)
con = sqlite3.connect('movie.db')
cur = con.cursor()
sql = 'select instroduction from main.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)
print(len(string))
img = Image.open(r'.\static\assets\img\tree.jpg') #打开遮罩图片
img_array = np.array(img) #将图片转换为数组
wc = WordCloud(
background_color='white',
mask = img_array,
font_path="兰亭特黑长简.TTF"
)
wc.generate_from_text(string)
#绘制图片
fig = plt.figure(1)
plt.imshow(wc)
plt.axis('off')
#plt.show() #现实生成的词云图片
#输出词云图片到文件
plt.savefig(r'.\static\assets\img\word.jpg')
|