​​​​ 微信好友头像拼图 | 苏生不惑的博客

微信好友头像拼图

微信机器人相信很多人在群里见过,大部分是基于itchat开发的,今天就用基于 itchat 的wxpy 将你的微信好友头像拼接成一张图片。

安装 wxpy

直接pip install wxpy 即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
λ wxpy -h
usage: wxpy [-h] [-c] [-q width] [-l level] [-s shell] [-v] [bot [bot ...]]

Run a wxpy-ready python console.

positional arguments:
bot One or more variable name(s) for bot(s) to init
(default: None).

optional arguments:
-h, --help show this help message and exit
-c, --cache Cache session(s) for a short time, or load session(s)
from cache (default: disabled).
-q width, --console_qr width
The width for console_qr (default: None).
-l level, --logging_level level
Logging level (default: INFO).
-s shell, --shell shell
Specify which shell to use: ipython, bpython, or
python (default: the first available).
-v, --version Show version and exit.

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-

from wxpy import *
import math
from PIL import Image
import os
print('双击exe执行,用微信扫描弹出的二维码,确认后等待一段时间,会在本地生成合成文件mywechat.png,有问题@苏生不惑')
#
def creat_filepath():
avatar_dir = os.getcwd() + "\\wechat_pic\\"
if not os.path.exists(avatar_dir):
os.mkdir(avatar_dir)
return avatar_dir


def save_avatar(avatar_dir):
#
bot = Bot()
friends = bot.friends(update=True)
print(friends.stats_text())

# 下载
num = 0
for friend in friends:
friend.get_avatar(avatar_dir + '\\' + str(num) + ".jpg")
print('好友昵称:%s' % friend.nick_name)
num = num + 1


def joint_avatar(path):
#
length = len(os.listdir(path))
#
image_size = 2560
#
each_size = math.ceil(2560 / math.floor(math.sqrt(length)))
#
x_lines = math.ceil(math.sqrt(length))
y_lines = math.ceil(math.sqrt(length))
image = Image.new('RGB', (each_size * x_lines, each_size * y_lines))
x = 0
y = 0
for (root, dirs, files) in os.walk(path):
for pic_name in files:

try:
with Image.open(path + pic_name) as img:
img = img.resize((each_size, each_size))
image.paste(img, (x * each_size, y * each_size))
x += 1
if x == x_lines:
x = 0
y += 1
except IOError:
print("头像读取失败")

img = image.save(os.getcwd() + "/mywechat.png")
print('微信好友头像拼接完成!')


if __name__ == '__main__':
avatar_dir = creat_filepath()
save_avatar(avatar_dir)
joint_avatar(avatar_dir)

使用

python wechat.py
将会弹出二维码,打开微信扫描登录,如果你的好友过千的话需要等待一段时间。
image.png

1
2
Login successfully as 风吹麦浪
好友昵称:风吹麦浪

image.png

效果

最后生成了所有好友的微信头像拼图,我的好友才100多人,所以生成的头像还能看清,如果有几千好友的话头像会很小,需要放大才能看到。

拼图

所有好友的微信头像都下载到本地了。
所有好友头像

赶紧发条朋友圈去装逼吧,哈哈。

如果你不会代码的话,我生成了一个EXE文件,只需要在电脑双击执行这个EXE就能自动拼图了。
image.png

关注本公众号,回复 微信拼图 获取EXE下载链接,暂时只支持windows系统。

扫描二维码关注