前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >用python一键生成你的微信好友头像墙

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

作者头像
Python编程与实战
发布2019-12-03 13:29:15
1.1K0
发布2019-12-03 13:29:15
举报

前言

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

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

程序使用教程

1.获取 .exe 程序
2.在windows上点击运行后,会弹出一个微信登陆的二维码,用手机微信扫描,确认登录。
3.登陆成功后,程序会显示正在保存的头像,最后会在程序运行的目录生成一张 all.png 的图片

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

代码教程

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

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

虚拟环境

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

2. 在pycharm 中导入刚才建好的虚拟环境
3.需要安装的库:

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

4. 接下来就是写代码了

微信登陆部分代码

代码语言:javascript
复制
 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  # 合成头像的时候需要用到

合成图像代码

代码语言:javascript
复制
 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!')

核心代码完成后,将两部分合一起再导入需要的包,就完事了

源码在此

代码语言:javascript
复制
 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的程序就更简单了 在命令行中运行下面的命令即可

代码语言:javascript
复制
1pyinstaller -F F:\wx\wx.py

运行成功后,会在倒数第二行显示生成程序的保存路径

好了,以上就是两种用python合成微信好友头像的方法

合成之后,可以发到自己的朋友圈,让别人来找找自己的头像在哪,顺便自己还能装个逼,哈哈~`


本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-04-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python编程与实战 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 程序使用教程
    • 1.获取 .exe 程序
      • 2.在windows上点击运行后,会弹出一个微信登陆的二维码,用手机微信扫描,确认登录。
        • 3.登陆成功后,程序会显示正在保存的头像,最后会在程序运行的目录生成一张 all.png 的图片
        • 代码教程
          • 1. 首先新建一个虚拟环境。为什么要虚拟环境?怎么建虚拟环境? 我之前的文章有写,去历史消息翻翻就能找到
            • 2. 在pycharm 中导入刚才建好的虚拟环境
              • 3.需要安装的库:
                • 4. 接下来就是写代码了
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档