1. 程式人生 > >Python獲取微信好友簽名生成詞雲

Python獲取微信好友簽名生成詞雲

tps targe fan generate lis 制作 dom white print

‘‘‘
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

Python獲取微信好友簽名生成詞雲