Python3 生成微信好友头像的图片合集

本文介绍了一种使用Python脚本从微信好友列表中下载头像,并将它们拼接成一张大图的方法。通过itchat库登录微信,获取好友列表,下载头像,计算拼接参数,最终生成一张包含所有好友头像的图像。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

刚才在github上看到一个大神写的生成微信好友图像集合的脚本
自己运行了一下发现挺好玩的

原帖地址:https://github.com/aloneZERO/py-party/tree/master/wechat-imgs
#!python3
# coding: utf-8

import itchat
import os

from PIL import Image
import math


# 首先登陆python版本微信itchat,生成二维码
# itchat.auto_login(enableCmdQR=True)
itchat.auto_login()

# 获取好友列表
friends = itchat.get_friends(update=True)[0:]

# 以自己的用户名加密码创建文件夹来存储图片
user = friends[0]["UserName"]
print("User Code: "+user)
os.mkdir(user)

# 使用itchat的get_head_img(userName=none)函数
# 爬取好友列表的头像,并下载到本地
num = 0
for i in friends:
    img = itchat.get_head_img(userName=i["UserName"])
    with open(user + "/" + str(num) + ".jpg", 'wb') as fileImage:
        fileImage.write(img)
    num += 1

# 计算好友数量
pics = os.listdir(user)
numPic = len(pics)
print("好友总数:"+str(numPic))

# 计算每张头像缩小后的边长(默认为正方形)
eachsize = int(math.sqrt(float(640 * 640) / numPic))
# print(eachsize)

# 计算合成图片每一边分为多少小边
numline = int(640 / eachsize)
toImage = Image.new('RGBA', (640, 640))
# print(numline)

# 缩小并拼接图片
x, y = 0, 0
for i in pics:
    try:
        # 打开图片
        img = Image.open(user + "/" + i)
    except IOError:
        print("Error: 没有找到文件或读取文件失败")
    else:
        # 缩小图片
        img = img.resize((eachsize, eachsize), Image.ANTIALIAS)
        # 拼接图片
        toImage.paste(img, (x * eachsize, y * eachsize))
        x += 1
        if x == numline:
            x = 0
            y += 1

# 保存拼接好的图片
# 通过文件助手发送给自己
toImage.save("funny.jpg")
itchat.send_image("funny.jpg", 'filehelper')
print("好友头像拼接完毕,快去查看吧~")

转载于:https://www.cnblogs.com/xuyuQAQ/p/8426021.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值