Fork me on Gitee

itchat娱乐

模块,图片自行安装选择

1.制作云词

 1 from wordcloud import WordCloud
 2 import jieba
 3 from scipy.misc import imread
 4 import itchat
 5 import re
 6 
 7 
 8 def getfriendsinfo():
 9     itchat.auto_login(hotReload=True)
10     friends = itchat.get_friends(update=True)[1:]
11     province = []
12     signatures = []
13     for friend in friends:
14         signature = friend['Signature'].strip().replace('span', '').replace('class', '').replace('emoji', '')
15         rep = re.compile('1f\d+\w*|[<>/=]')
16         signature = rep.sub('', signature)
17         signatures.append(signature)
18     return signatures
19 
20 
21 # 云词
22 def drawSignatureWC(signature):
23     cut_text = " ".join(jieba.cut(signature))
24     # 选择底板
25     color_mask = imread('python.png')
26     wc = WordCloud(
27         font_path=r'C:\Windows\Fonts\simhei.ttf',
28         background_color='white',
29         mask=color_mask,
30         max_words=2000,
31         max_font_size=60
32     )
33     wc.generate(cut_text)
34     wc.to_file('mark.jpg')
35 
36 
37 if __name__ == '__main__':
38     signatures = getfriendsinfo()
39     drawSignatureWC(''.join(signatures))

 

2.发消息

 1 import itchat
 2 
 3 itchat.auto_login(hotReload=True)
 4 
 5 friends_list = itchat.get_friends(update=True)
 6 name = itchat.search_friends(name=u'')
 7 target = name[0]['UserName']
 8 
 9 message = '这是一条代码'
10 for i in range(10):
11     itchat.send(message, target)

3.好友头像拼接

 1 import itchat
 2 import math
 3 import os
 4 import PIL.Image as Image
 5 
 6 # 给auto_login方法传入值为真的hotReload.即使程序关闭,一定时间内重新开启也可以不用重新扫码
 7 itchat.auto_login(hotReload=True)
 8 friends = itchat.get_friends(update=True)
 9 
10 # 下载所有好友的头像图片
11 num = 0
12 for i in friends:
13     img = itchat.get_head_img(i["UserName"])
14     with open('headImg/' + str(num) + ".jpg", 'wb') as f:
15         f.write(img)
16         f.close()
17         num += 1
18 # 获取文件夹内的文件个数
19 length = len(os.listdir('headImg'))
20 # 根据总面积求每一个的大小
21 each_size = int(math.sqrt(float(810 * 810) / length))
22 # 每一行可以放多少个
23 lines = int(810 / each_size)
24 # 生成白色背景新图片
25 image = Image.new('RGBA', (810, 810), 'white')
26 x = 0
27 y = 0
28 for i in range(0, length):
29     try:
30         img = Image.open('headImg/' + str(i) + ".jpg")
31     except IOError:
32         # print(i)
33         # print("Error")
34         pass
35     else:
36         img = img.resize((each_size, each_size), Image.ANTIALIAS)  # resize image with high-quality
37         image.paste(img, (x * each_size, y * each_size))
38         x += 1
39         if x == lines:
40             x = 0
41             y += 1
42 image.show()

 

posted @ 2018-09-09 21:50  MARK+  阅读(201)  评论(0编辑  收藏  举报