itchat库初探--微信好友全头像的拼接

 
itchat库初探--微信好友全头像的拼接
 

代码:

 1 import itchat
 2 import math
 3 import PIL.Image as Image
 4 import os
 5 
 6 itchat.auto_login()
 7 friends = itchat.get_friends(update=True)[0:]
 8 user = friends[0]["UserName"]
 9 
10 num = 0
11 for i in friends:
12     img = itchat.get_head_img(userName=i["UserName"])
13     fileImage = open(r文件夹 + "/" + str(num) + ".jpg",wb)
14     fileImage.write(img)
15     fileImage.close()
16     num += 1
17 
18 ls = os.listdir(r文件夹)
19 each_size = int(math.sqrt(float(640*640)/len(ls)))
20 lines = int(640/each_size)
21 image = Image.new(RGBA, (640, 640))
22 x = 0
23 y = 0
24 for i in range(0,len(ls)+1):
25     try:
26         img = Image.open(r文件夹 + "/" + str(i) + ".jpg")
27     except IOError:
28         print("Error")
29     else:
30         img = img.resize((each_size, each_size), Image.ANTIALIAS)
31         image.paste(img, (x * each_size, y * each_size))
32         x += 1
33         if x == lines:
34             x = 0
35             y += 1
36 image.save(r文件夹 + "/" + "all.jpg")
37 itchat.send_image(r文件夹 + "/" + "all.jpg", filehelper)

代码中的文件夹”是需要创建一个文件夹,并用该文件夹的路径替换调文件夹

代码运行需要安装两个库

1 pip install itchat
2 pip install pillow

如果安装python的时候pip安装选项没打√ ,就先安装pip。Python和pip的安装

itchat库初探--微信好友全头像的拼接

itchat官方介绍

代码运行过程中会出现登录二维码,用微信扫一下,你就可以看到处理的进度。稍后你的微信文件传输助手就会收到拼接好的头像图片。

itchat库初探--微信好友全头像的拼接

上一篇:微信js jdk 的配置


下一篇:小北微信小程序之小白教程系列之 -- 样式(WXSS)