#导入模块
import jieba#分词
from matplotlib import pyplot as plt#绘图数据可视化
from wordcloud import WordCloud#词云图
import numpy as np#矩阵
import sqlite3#数据库
from PIL import Image#图片处理
#准备词云图需要的文字
con = sqlite3.connect('movie.bd')
cur = con.cursor()#获取游标
sql = 'select instroduction from movie250'
data = cur.execute(sql)#执行sql
text = " "
for item in data:
text = text + item[0]
print(text)
cur.close()#关闭数据库
con.close()
#分词
cut = jieba.cut(text)
string = ' '.join(cut)
print(string)
print(len(string))
#绘图准备
img = Image.open('6.jpg')
img_array = np.array(img)
wc = WordCloud(
background_color = 'white',
mask = img_array,
font_path = "STXINWEI.TTF"
)
wc.generate_from_text(string)#切好词放进去
#绘制图片
fig = plt.figure(1)
plt.rcParams['font.sans-serif'] = 'SimHei'#设置字体
plt.imshow(wc)#按词云显示
plt.axis('off')#是否显示坐标
plt.show()#展示生成的词云图
plt.savefig('8.png',dpi = 800)
|