前言
用 python 代码写了一个一键合成微信好友头像墙的程序,效果如下:
不会写代码?没关系!只要你会使用电脑就 ok! 因为除了用代码方式生成外,还建了一个 .exe 的程序,在电脑点击运行就完事了 下面分别详细的给大家讲解是如何实现的
当看到 "所有的微信头像已合成,请查阅all.png!" 的时候,你要的头像墙就在 [wxImages] 文件夹里面
代码其实很简单,主要是做起来觉得很有意义,如果你会python基础,再加上下面的讲解,你也可以的!
虚拟环境
虚拟环境的名字随意取,我取的是 [“wx”]
wxpy 用来操作微信的,除了获取头像,还能给好友发消息,具体可查看官方文档 pillow <=4.2.1 处理头像 pyinstaller 将代码打包成 .exe 程序的
微信登陆部分代码
1@staticmethod
2 def get_image():
3 path = os.path.abspath(".") #当前目录
4 bot = Bot() # 机器人对象
5 friends = bot.friends(update=True)
6 dirs = path + "\\wxImages" # 微信头像保存的路径
7 if not os.path.exists(dirs):
8 os.mkdir("wxImages")
9
10 index = 0
11 for friend in friends:
12 print(f"正在保存{friend.nick_name}的微信头像")
13 friend.get_avatar(dirs + "\\" + f"{str(index)}.jpg")
14 index += 1
15
16 return dirs # 合成头像的时候需要用到
合成图像代码
1 @staticmethod
2 def composite_image(dirs):
3 images_list = os.listdir(dirs)
4 images_list.sort(key=lambda x: int(x[:-4])) # 根据头像名称排序
5 length = len(images_list) # 头像总数
6 image_size = 2560 #
7 # 每个头像大小
8 each_size = math.ceil(image_size / math.floor(math.sqrt(length)))
9 lines = math.ceil(math.sqrt(length)) # 列数
10 rows = math.ceil(math.sqrt(length)) # 行数
11 image = Image.new('RGB', (each_size * lines, each_size * rows))
12 row = 0
13 line = 0
14 os.chdir(dirs) # 切换工作目录
15 for file in images_list: # 遍历每个头像
16 try:
17 with Image.open(file) as img:
18 img = img.resize((each_size, each_size))
19 image.paste(img, (line * each_size, row * each_size))
20 line += 1
21 if line == lines: # 一行填满后开始填下一行
22 line = 0
23 row += 1
24 except IOError:
25 print(f"头像{file}异常,请查看")
26 continue
27
28 img = image.save(os.getcwd() + "/all.png") # 将合成的头像保存
29 if not img:
30 print('所有的微信头像已合成,请查阅all.png!')
核心代码完成后,将两部分合一起再导入需要的包,就完事了
源码在此
1# coding: utf-8
2from wxpy import Bot, Chat
3import math
4import os
5from PIL import Image
6
7class WxFriendImage(Chat):
8 @staticmethod
9 def get_image():
10 path = os.path.abspath(".")
11 bot = Bot() # 机器人对象
12 friends = bot.friends(update=True)
13
14 dirs = path + "\\wxImages"
15 if not os.path.exists(dirs):
16 os.mkdir("wxImages")
17
18 index = 0
19 for friend in friends:
20 print(f"正在保存{friend.nick_name}的微信头像")
21 friend.get_avatar(dirs + "\\" + f"{str(index)}.jpg")
22 index += 1
23
24 return dirs
25
26 @staticmethod
27 def composite_image(dirs):
28 images_list = os.listdir(dirs)
29 images_list.sort(key=lambda x: int(x[:-4])) # 根据头像名称排序
30 length = len(images_list) # 头像总数
31 image_size = 2560
32 # 每个头像大小
33 each_size = math.ceil(image_size / math.floor(math.sqrt(length)))
34 lines = math.ceil(math.sqrt(length)) # 列数
35 rows = math.ceil(math.sqrt(length)) # 行数
36 image = Image.new('RGB', (each_size * lines, each_size * rows))
37 row = 0
38 line = 0
39 os.chdir(dirs)
40 for file in images_list:
41 try:
42 with Image.open(file) as img:
43 img = img.resize((each_size, each_size))
44 image.paste(img, (line * each_size, row * each_size))
45 line += 1
46 if line == lines:
47 line = 0
48 row += 1
49 except IOError:
50 print(f"头像{file}异常,请查看")
51 continue
52 img = image.save(os.getcwd() + "/all.png")
53 if not img:
54 print('所有的微信头像已合成,请查阅all.png!')
55def main():
56 dirs = WxFriendImage.get_image()
57 WxFriendImage.composite_image(dirs)
58if __name__ == '__main__':
59 main()
可以将代码复制到自己的编译器里面运行,效果是一样的。
至于打包成 .exe的程序就更简单了 在命令行中运行下面的命令即可
1pyinstaller -F F:\wx\wx.py
运行成功后,会在倒数第二行显示生成程序的保存路径
好了,以上就是两种用python合成微信好友头像的方法
合成之后,可以发到自己的朋友圈,让别人来找找自己的头像在哪,顺便自己还能装个逼,哈哈~`