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

python生成微信好友头像图

作者头像
溪初桾
发布2020-03-12 16:22:09
1.6K0
发布2020-03-12 16:22:09
举报
文章被收录于专栏:溪溪技术专栏溪溪技术专栏

更新本地pip

python -m pip install --upgrade pip

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

代码语言:javascript
复制
# 这里我本机已经安装
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

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

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

项目源代码

代码语言:javascript
复制
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文件夹下
代码语言:javascript
复制
# 如下为控制台部分输出
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_phone.jpg

程序获取到的结果

wx_pic_get.png
wx_pic_get.png

wx_pic_get.png

最终拼接后的结果


wx.png
wx.png

wx.png

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-04-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 更新本地pip
  • 安装核心依赖库
  • 项目源代码
  • 直接运行py文件就行
  • 运行结果及说明
  • 手机微信截图结果
  • 程序获取到的结果
  • 最终拼接后的结果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档