Python获取微信好友签名生成词云

'''
pip install wxpy
pip install matplotlib  # 如果下载超时,就换源下载:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple matplotlib

pip install wordcloud
pip install Pillow
pip install numpy
pip install jieba
pip install scipy       # 处理图像   # pip install -i https://pypi.tuna.tsinghua.edu.cn/simple scipy

'''


import re
import itchat
import jieba
import matplotlib.pyplot as plt
from wordcloud import WordCloud, ImageColorGenerator
from scipy.misc import imread



# 1. 登录,获取好友列表,用手机扫二维码登录

itchat.login()
# 2. 获取好友列表
friends = itchat.get_friends(update=True)[0:]
# print(friends)


# 3. 去除所有这些符号
tList = []
for i in friends:
    # 获取个性签名
    signature = i['Signature'].strip().replace('span', '').replace('class', '').replace('emoji', '')
    #正则匹配过滤掉emoji表情, 例如emoji1f33f等
    rep = re.compile("1f\d.+")
    signature = rep.sub('', signature)
    tList.append(signature)

# 制作词云

text = ''.join(tList)

wordlist_jieba = jieba.cut(text, cut_all=True)
wl_space_split = ' '.join(wordlist_jieba)
# print(wl_space_split)


# 用于生成配色方案的图
back_color = imread('mao.jpg')

# 词云
my_wordcloud = WordCloud(background_color='white',  # 背景颜色
               max_words=2000,  # 最大词数
               mask=back_color,  # 以该参数值作图绘制词云,这个参数不为空时,width和height会被忽略
               max_font_size=100,  # 显示字体的最大值
               # stopwords=STOPWORDS.add('中国'),  # 使用内置的屏蔽词,再添加'中国'
               font_path='/Users/guohongjun/Library/Fonts/simfang.ttf',  # 指定字体文件 解决显示口字型乱码问题,
               random_state=42,  # 为每个词返回一个PIL颜色
               # width=1000,  # 图片的宽
               # height=860  #图片的长
               )

# 用wl_space_split生成词云
my_wordcloud.generate(wl_space_split)

# 基于彩色图像 生成响应的色彩
image_colors = ImageColorGenerator(back_color)
# 显示图片
# plt.imshow(my_wordcloud)
# 关闭坐标轴
# plt.axis('off')
# 绘制词云
plt.figure()
plt.imshow(my_wordcloud.recolor(color_func=image_colors))
plt.axis('off')
# 保存图片
my_wordcloud.to_file('ciyun.png')




see also:

  https://www.jianshu.com/p/a60b6ef1e6f6

猜你喜欢

转载自www.cnblogs.com/Neeo/p/10101595.html