import jieba
import wordcloud
import matplotlib.pyplot as plt
import imageio
import pymysql
from sqlalchemy.dialects import mysql
from wordcloud import ImageColorGenerator

def connect_mysql():
# 建立数据库连接
    conn = pymysql.Connect(host='localhost',port=3306,user='root',passwd='123456',db='movies_comment',charset='utf8')
    cursor = conn.cursor()
# print(conn)
# print(cursor)
# 1、从数据库中查询
# sql="INSERT INTO userinfor(user_name,pass_word)"
    sql = "SELECT movie_type FROM movie_maoyan where production_country !='中国大陆'"
# cursor执行sql语句
    txt2 = cursor.execute(sql)
    txt3 = cursor.fetchall()
    print(txt3)
    return txt3

mk = imageio.imread('地图.jpg')

w = wordcloud.WordCloud(font_path='MicrosoftYaqiHeiLight-2.ttf',
                        mask=mk,contour_width=1,scale=15)

#f = open('电影名句.txt',encoding='utf-8')




#txt = f.read()
txt1 = connect_mysql()
txt = str(txt1)



#txtlist=jieba.lcut(txt1)
#string = " ".join(txtlist)
w.generate(txt)



w.to_file('output4.jpg')
