您的位置:首页 > 编程语言 > Python开发

用python一键生成你的微信好友头像墙

2020-03-05 17:05 465 查看

导语

你千万别跟任何人谈任何事情。你只要一谈起,就会想念起每一个人来,我只知道我很想念我所谈到的每一个人。

          ——J·D·塞林格《麦田里的守望者》

前言

用 python 代码写了一个一键合成微信好友头像墙的程序,效果如下:

不会写代码?没关系!只要你会使用电脑就 ok!
因为除了用代码方式生成外,还建了一个 .exe 的程序,在电脑点击运行就完事了
下面分别详细的给大家讲解是如何实现的

程序使用教程

1.公众号后台回复 “wx”即可获取 .exe 程序


2.在windows上点击运行后,会弹出一个微信登陆的二维码,用手机微信扫描,确认登录。

3.登陆成功后,程序会显示正在保存的头像,最后会在程序运行的目录生成一张 all.png 的图片

当看到 "所有的微信头像已合成,请查阅all.png!" 的时候,你要的头像墙就在 [wxImages] 文件夹里面

代码教程

代码其实很简单,主要是做起来觉得很有意义,如果你会python基础,再加上下面的讲解,你也可以的!

1. 首先新建一个虚拟环境。为什么要虚拟环境?怎么建虚拟环境? 我之前的文章有写,去历史消息翻翻就能找到

虚拟环境

虚拟环境的名字随意取,我取的是 [“wx”]

2. 在pycharm 中导入刚才建好的虚拟环境

3.需要安装的库:

wxpy
用来操作微信的,除了获取头像,还能给好友发消息,具体可查看官方文档pillow <=4.2.1
处理头像pyinstaller
将代码打包成 .exe 程序的

4. 接下来就是写代码了

微信登陆部分代码

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合成微信好友头像的方法合成之后,可以发到自己的朋友圈,让别人来找找自己的头像在哪,顺便自己还能装个逼,哈哈~`
觉得对你有用,就帮忙点个赞呗…

看完本文有收获?请转发分享给更多的人
关注[Python编程与实战],学习更多实战技能

Facebook的广告也许正在窃听你的对话

python数据可视化神器--pyecharts 快速入门

【视频教程】011.__init__和__new__的使用

当 Python 中混进一只薛定谔的猫……

LeetCode - 001

和张哥的那些天,互联网人的潜规则

那些年错过的并发知识!

超强汇总:学习Python列表,只需这篇文章就够了

记一次群聊吃瓜引发的JS破解教程

爬虫之线程池 ThreadPoolExecutor 的用法及实战

▼立即加星标,每天看好文▼

1

2

极客学习空间

新媒体人都在关注

喜欢,就为我标星

我今天才知道,我之所以漂泊就是在向你靠近。--《廊桥遗梦》

  • 点赞
  • 收藏
  • 分享
  • 文章举报
aaz913648653 发布了5 篇原创文章 · 获赞 0 · 访问量 385 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: