点击上方“Python3X”,选择“置顶或者星标”
第一时间收到精彩推送!
先看一下2000位好友的大合照。
有没有犯密集恐惧症?这并不震撼,如果你有 5000 位好友的话,做出来的图看着会更刺激些。
看完了图,你可能想知道这个图咋做出来的,不会是我闲着无聊把把好友头像一个个保存下来再用 PS 拼的吧?
自然不是了,Python 做的,是不是觉得没有 Python 干不了的事儿。其实,这种图很早就有人玩过了,不过下面还是来说说怎么做出来,这样你可以做一个自己的微信好友图片墙,拿去朋友圈装装逼。
有两种方法,一种简单的,不用接触 Python 代码,一种稍微复杂点,需要写代码。
先说简单的方法,只需要两步:运行程序然后扫微信二维码就行了。剩下的交给程序自己蹦跶,泡杯茶在电脑前等待几分钟左右就可以得到图片,具体的等待时间视微信好友数量而不同,我近 2000 好友,用时 10 分钟左右。
一个简单的操作示意图:
几分钟后就可以得到上面的图片了。
其实到这儿就完了,是不是很简单。
你要感兴趣怎么实现的,可以往下看用 Python 代码怎么实现的,代码不长,60 行就可以搞定。
核心是利用三个个库:
程序通过三个函数实现,第一个 creat_filepath 函数生成图片下载文件路径,第二个 save_avatar 函数循环获取微信好友头像然后保存到本地,第三个 joint_avatar 函数就是把头像拼接成一张大图。
完整代码如下:
1# -*- coding: utf-8 -*- 2from wxpy import * 3import math 4from PIL import Image 5import os 6 7# 创建头像存放文件夹 8def creat_filepath(): 9 avatar_dir = os.getcwd() + "\\wechat\\"10 if not os.path.exists(avatar_dir):11 os.mkdir(avatar_dir)12 return avatar_dir1314# 保存好友头像15def save_avatar(avatar_dir):16 # 初始化机器人,扫码登陆17 bot = Bot()18 friends = bot.friends(update=True)19 num = 020 for friend in friends:21 friend.get_avatar(avatar_dir + '\\' + str(num) + ".jpg")22 print('好友昵称:%s' % friend.nick_name)23 num = num + 12425# 拼接头像26def joint_avatar(path):27 # 获取文件夹内头像个数28 length = len(os.listdir(path))29 # 设置画布大小30 image_size = 256031 # 设置每个头像大小32 each_size = math.ceil(2560 / math.floor(math.sqrt(length)))33 # 计算所需各行列的头像数量34 x_lines = math.ceil(math.sqrt(length))35 y_lines = math.ceil(math.sqrt(length))36 image = Image.new('RGB', (each_size * x_lines, each_size * y_lines))37 x = 038 y = 039 for (root, dirs, files) in os.walk(path):40 for pic_name in files:41 # 增加头像读取不出来的异常处理42 try:43 with Image.open(path + pic_name) as img:44 img = img.resize((each_size, each_size))45 image.paste(img, (x * each_size, y * each_size))46 x += 147 if x == x_lines:48 x = 049 y += 150 except IOError:51 print("头像读取失败")5253 img = image.save(os.getcwd() + "/wechat.png")54 print('微信好友头像拼接完成!')5556if __name__ == '__main__':57 avatar_dir = creat_filepath()58 save_avatar(avatar_dir)59 joint_avatar(avatar_dir)# -*- coding: utf-8 -*-
2from wxpy import *
3import math
4from PIL import Image
5import os
6
7# 创建头像存放文件夹
8def creat_filepath():
9 avatar_dir = os.getcwd() + "\\wechat\\"
10 if not os.path.exists(avatar_dir):
11 os.mkdir(avatar_dir)
12 return avatar_dir
13
14# 保存好友头像
15def save_avatar(avatar_dir):
16 # 初始化机器人,扫码登陆
17 bot = Bot()
18 friends = bot.friends(update=True)
19 num = 0
20 for friend in friends:
21 friend.get_avatar(avatar_dir + '\\' + str(num) + ".jpg")
22 print('好友昵称:%s' % friend.nick_name)
23 num = num + 1
24
25# 拼接头像
26def joint_avatar(path):
27 # 获取文件夹内头像个数
28 length = len(os.listdir(path))
29 # 设置画布大小
30 image_size = 2560
31 # 设置每个头像大小
32 each_size = math.ceil(2560 / math.floor(math.sqrt(length)))
33 # 计算所需各行列的头像数量
34 x_lines = math.ceil(math.sqrt(length))
35 y_lines = math.ceil(math.sqrt(length))
36 image = Image.new('RGB', (each_size * x_lines, each_size * y_lines))
37 x = 0
38 y = 0
39 for (root, dirs, files) in os.walk(path):
40 for pic_name in files:
41 # 增加头像读取不出来的异常处理
42 try:
43 with Image.open(path + pic_name) as img:
44 img = img.resize((each_size, each_size))
45 image.paste(img, (x * each_size, y * each_size))
46 x += 1
47 if x == x_lines:
48 x = 0
49 y += 1
50 except IOError:
51 print("头像读取失败")
52
53 img = image.save(os.getcwd() + "/wechat.png")
54 print('微信好友头像拼接完成!')
55
56if __name__ == '__main__':
57 avatar_dir = creat_filepath()
58 save_avatar(avatar_dir)
59 joint_avatar(avatar_dir)
可以直接在运行程序文件,也可以用 Pyinstaller 文件打包后运行。这里额外说一下 pyinstaller 打包的方法和闭坑指南。
不要直接在系统中用 pyinstaller 打包,否则打包出来的 exe 文件会很大。建议在虚拟环境中打包,打包出来的 exe 文件会小很多, 10MB 左右。
虚拟环境创建很简单,简单说一下步骤:
1 安装 pipenv 和 pyinstaller 包,用于后续创建虚拟环境和打包程序:
1pip install pipenv2pip install pyinstaller # 已安装就不用安装了
2pip install pyinstaller # 已安装就不用安装了
2 选择一个合适的目录作为 Python 虚拟环境,运行:
1pipenv install # 创建虚拟环境2pipenv shell # 创建好后,进入虚拟环境# 创建虚拟环境
2pipenv shell # 创建好后,进入虚拟环境
3 安装程序引用的库,上面程序引用了四个库:wxpy、math、os 和 PIL,一行代码就可以完成安装。
1pipenv install wxpy math os
4 这里要额外注意 PIL 的安装,现在不用 PIL 库,而是用 Pillow 库取代,所以安装 Pillow 库就行。但不要安装最新的 6.0.0 版本,否则可能会遇到各种错误,例如:PIL 无法识别下载的 jpg 头像文件。
1OSError: cannot identify image file <ImageFieldFile: images
正确的安装方法是安装低版本,经尝试安装 4.2.1 版本没有问题,安装命令:
1pipenv install Pillow==4.2.14.2.1
5 然后打包程序就可以了:
1pyinstaller -F C:\Users\sony\Desktop\wechat_avatar.py 2# 程序路径要改成你电脑上的路径3# -F 表示生成单个 exe 文件,方便运行
2# 程序路径要改成你电脑上的路径
3# -F 表示生成单个 exe 文件,方便运行
运行如下:

运行命令,1 分钟左右若显示 successfully 字样表示程序打包成功:
接着在程序目录下找到 wechat_avatar.exe
文件,然后按照第一种方法那样运行就行了。
以上就是用 Python 制作微信好友图片墙的装逼指南。
后台回复:「好友」就可以得到 exe 文件和 Python 程序。
完
长按二维码关注
公众号「Python3X」