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

微信好友头像拼图

作者头像
苏生不惑
发布2019-08-17 13:28:17
3.1K0
发布2019-08-17 13:28:17
举报
文章被收录于专栏:苏生不惑苏生不惑

微信机器人相信很多人在群里见过,大部分是基于itchat开发的,今天就用基于 itchat 的wxpy 将你的微信好友头像拼接成一张图片。

安装 wxpy

直接pip install wxpy 即可。

代码语言:javascript
复制
λ wxpy -husage: wxpy [-h] [-c] [-q width] [-l level] [-s shell] [-v] [bot [bot ...]]
Run a wxpy-ready python console.
positional arguments:  bot                   One or more variable name(s) for bot(s) to init                        (default: None).
optional arguments:  -h, --help            show this help message and exit  -c, --cache           Cache session(s) for a short time, or load session(s)                        from cache (default: disabled).  -q width, --console_qr width                        The width for console_qr (default: None).  -l level, --logging_level level                        Logging level (default: INFO).  -s shell, --shell shell                        Specify which shell to use: ipython, bpython, or                        python (default: the first available).  -v, --version         Show version and exit.

源码

代码语言:javascript
复制
# -*- coding: utf-8 -*-
from wxpy import *import mathfrom PIL import Imageimport osprint('双击exe执行,用微信扫描弹出的二维码,确认后等待一段时间,会在本地生成合成文件mywechat.png,有问题@苏生不惑')# def creat_filepath():    avatar_dir = os.getcwd() + "\\wechat_pic\\"    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)    print(friends.stats_text())
    # 下载    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() + "/mywechat.png")    print('微信好友头像拼接完成!')

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

使用

python wechat.py 将会弹出二维码,打开微信扫描登录,如果你的好友过千的话需要等待一段时间。

代码语言:javascript
复制
Login successfully as 风吹麦浪好友昵称:风吹麦浪

效果

最后生成了所有好友的微信头像拼图,我的好友才100多人,所以生成的头像还能看清,如果有几千好友的话头像会很小,需要放大才能看到。

所有好友的微信头像都下载到本地了。

赶紧发条朋友圈去装逼吧,哈哈。

如果你不会使用代码的话,我生成了一个EXE文件,只需要在电脑双击执行这个EXE就能自动拼图了。

关注本公众号,回复 微信拼图 获取EXE下载链接,暂时只支持windows系统。

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

本文分享自 苏生不惑 微信公众号,前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装 wxpy
  • 源码
  • 使用
  • 效果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档