微信好友签名词云展示
一:介绍
1.主要是按照百度上的流程一步一个脚印
2.做好相关包安装和配置
3.复制粘贴代码,然后进行修改
4.一般都是修改文件放置的位置
5.此处主要是图片所处的位置,事先下载好图片,并放到与代码一个文件夹下。
6.生成的图片也会在此文件夹下
源代码:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import itchat
import re
import jieba
def echart_pie(friends):
total = len(friends) - 1
male = female = other = 0
for friend in friends[1:]:
sex = friend["Sex"]
if sex == 1:
male += 1
elif sex == 2:
female += 1
else:
other += 1
from echarts import Echart, Legend, Pie
chart = Echart('%s的微信好友性别比例' % (friends[0]['Name']), 'from WeChat')
chart.use(Pie('WeChat',
[{'value': male, 'name': '男性 %.2f%%' % (float(male) / total * 100)},
{'value': female, 'name': '女性 %.2f%%' % (float(female) / total * 100)},
{'value': other, 'name': '其他 %.2f%%' % (float(other) / total * 100)}],
radius=["50%", "70%"]))
chart.use(Legend(["male", "female", "other"]))
del chart.json["xAxis"]
del chart.json["yAxis"]
chart.plot()
def word_cloud(friends):
import matplotlib.pyplot as plt
from wordcloud import WordCloud, ImageColorGenerator
import PIL.Image as Image
import os
import numpy as np
d= os.path.dirname(__file__)
my_coloring = np.array(Image.open(os.path.join(d, "2.png")))
signature_list = []
for friend in friends:
signature = friend["Signature"].strip()
signature = re.sub("<span.*>", "", signature)
signature_list.append(signature)
raw_signature_string = ''.join(signature_list)
text = jieba.cut(raw_signature_string, cut_all=True)
target_signatur_string = ' '.join(text)
my_wordcloud = WordCloud(background_color="white", max_words=2000, mask=my_coloring,
max_font_size=40, random_state=42,
font_path=r"C:\Windows\Fonts\simhei.ttf").generate(target_signatur_string)
image_colors = ImageColorGenerator(my_coloring)
plt.imshow(my_wordcloud.recolor(color_func=image_colors))
plt.imshow(my_wordcloud)
plt.axis("off")
plt.show()
# 保存图片 并发送到手机
my_wordcloud.to_file(os.path.join(d, "wechat_cloud.png"))
itchat.send_image("wechat_cloud.png", 'filehelper')
itchat.auto_login(hotReload=True)
itchat.dump_login_status()
friends = itchat.get_friends(update=True)[:]
# echart_pie(friends)
word_cloud(friends)
参考:http://www.cnblogs.com/feixuelove1009/p/6950102.html