python生成微信好友头像图

信息好友头像聚合

请注意,本文编写于 172 天前,最后修改于 122 天前,其中某些信息可能已经过时。

更新本地pip

python -m pip install --upgrade pip

C:\Users\allms>python -m pip install --upgrade pip
Collecting pip
  Downloading https://files.pythonhosted.org/packages/f9/fb/863012b13912709c13cf5cfdbfb304fa6c727659d6290438e1a88df9d848/pip-19.1-py2.py3-none-any.whl (1.4MB)
    100% |████████████████████████████████| 1.4MB 30kB/s
Installing collected packages: pip
  Found existing installation: pip 19.0.3
    Uninstalling pip-19.0.3:
      Successfully uninstalled pip-19.0.3
Successfully installed pip-19.1

安装核心依赖库

  • wxpy 库,用于获取好友头像然后下载

pip install wxpy

# 这里我本机已经安装
C:\Users\allms>pip install wxpy
Requirement already satisfied: wxpy in d:\studay\python\lib\site-packages (0.3.9.8)
Requirement already satisfied: future in d:\studay\python\lib\site-packages (from wxpy) (0.17.1)
Requirement already satisfied: requests in d:\studay\python\lib\site-packages (from wxpy) (2.20.0)
Requirement already satisfied: itchat==1.2.32 in d:\studay\python\lib\site-packages (from wxpy) (1.2.32)
Requirement already satisfied: urllib3<1.25,>=1.21.1 in d:\studay\python\lib\site-packages (from requests->wxpy) (1.24)
Requirement already satisfied: idna<2.8,>=2.5 in d:\studay\python\lib\site-packages (from requests->wxpy) (2.7)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in d:\studay\python\lib\site-packages (from requests->wxpy) (3.0.4)
Requirement already satisfied: certifi>=2017.4.17 in d:\studay\python\lib\site-packages (from requests->wxpy) (2018.10.15)
Requirement already satisfied: pyqrcode in d:\studay\python\lib\site-packages (from itchat==1.2.32->wxpy) (1.2.1)
Requirement already satisfied: pypng in d:\studay\python\lib\site-packages (from itchat==1.2.32->wxpy) (0.0.19)
  • Pillow 库,用于拼接头像

pip install Pillow

C:\Users\allms>pip install Pillow
Requirement already satisfied: Pillow in d:\studay\python\lib\site-packages (5.4.1)
  • Pyinstaller 库,用来打包 Python 程序成 exe 文件

pip install Pyinstaller

C:\Users\allms>pip install Pyinstaller
Requirement already satisfied: Pyinstaller in d:\studay\python\lib\site-packages (3.4)
Requirement already satisfied: setuptools in d:\studay\python\lib\site-packages (from Pyinstaller) (39.0.1)
Requirement already satisfied: pefile>=2017.8.1 in d:\studay\python\lib\site-packages (from Pyinstaller) (2018.8.8)
Requirement already satisfied: macholib>=1.8 in d:\studay\python\lib\site-packages (from Pyinstaller) (1.11)
Requirement already satisfied: altgraph in d:\studay\python\lib\site-packages (from Pyinstaller) (0.16.1)
Requirement already satisfied: pywin32-ctypes in d:\studay\python\lib\site-packages (from Pyinstaller) (0.2.0)
Requirement already satisfied: future in d:\studay\python\lib\site-packages (from pefile>=2017.8.1->Pyinstaller) (0.17.1)

项目源代码

from wxpy import *
import math
from PIL import Image
import os

# 创建头像存放文件夹
def creat_filepath():
    avatar_dir = os.getcwd() + "\\wechat\\"
    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)
    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() + "/wechat.png")
    print('微信好友头像拼接完成!')

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

直接运行py文件就行

项目源文件 WeiXinPhoto.zip

运行结果及说明

  • 生成弹出微信授权登录的二维码,需要通过手机扫码授权登录
  • 扫码授权登录成功后,控制台提示Login successfully并且开始获取头像昵称,并下载用户头像到wechat文件夹下
# 如下为控制台部分输出
D:\Studay\Python\python.exe A:/wxPhoto/WeiXinPhoto.py
Getting uuid of QR code.
Downloading QR code.
Please scan the QR code to log in.
Please press confirm on your phone.
Loading the contact, this may take a little while.
Login successfully as 七分妙夏
好友昵称:七分妙夏
好友昵称:小磊
好友昵称:被分割的1/2
好友昵称:明明病
好友昵称:HCH
微信好友头像拼接完成!

Process finished with exit code 0

手机微信截图结果

wx_phone.jpg
wx_phone.jpg

程序获取到的结果

wx_pic_get.png
wx_pic_get.png

最终拼接后的结果


wx.png
wx.png

此处评论已关闭