‘‘‘pip install wxpypip install matplotlib # 如果下载超时,就换源下载:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple matplotlibpip install wordcloudpip install Pillowpip install numpypip install jiebapip install scipy # 处理图像 # pip install -i https://pypi.tuna.tsinghua.edu.cn/simple scipy‘‘‘import reimport itchatimport jiebaimport matplotlib.pyplot as pltfrom wordcloud import WordCloud, ImageColorGeneratorfrom 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("1fd.+") 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