微信好友头像

将所有微信好友头像集合在一张图片里

import itchat
import os
from PIL import Image
import math

def download_headimg(save_path):
    """
    该函数功能为下载所有好友头像
    """
    itchat.auto_login(hotReload=True)
    my_friends = itchat.get_friends(update=True)[0:]

    for friend in my_friends:
        head = itchat.get_head_img(userName=friend["UserName"])
        #img_file = open(save_path + friend['RemarkName'] + '.jpg', 'wb')
        try:
            img_file = open(save_path + friend['NickName'] + '.jpg', 'wb')
            img_file.write(head)
            img_file.close()
        except Exception as e:
            print(friend['NickName']+"---该好友头像导出出错")   # 找出导出头像失败的好友名字(NickName) 或者备注RemarkName
            continue

def splice_imgs(file_path, save_path):
    """
    该函数功能为拼接图片
    :param file_path: 源图片存储路径
    :param save_path: 拼接完成的图片存储路径
    """

    # 将所有头像的路径提取出来
    path_list = []
    for item in os.listdir(file_path):
        img_path = os.path.join(file_path, item)
        path_list.append(img_path)

    # 为拼凑成一个正方形图片,每行头像个数为总数的开平方取整
    line = int(math.sqrt(len(path_list)))

    # 新建待拼凑图片
    New_Image = Image.new('RGB', (128 * line, 128 * line))

    x = 0
    y = 0
    # 进行拼图
    for item in path_list:
        try:
            img = Image.open(item)
            img = img.resize((128, 128), Image.ANTIALIAS)
            New_Image.paste(img, (x * 128, y * 128))
            x += 1
        except IOError:
            print("第%d行,%d列文件读取失败!IOError:%s" % (y, x, item))
            x -= 1
        if x == line:
            x = 0
            y += 1
        if (x + line * y) == line * line:
            break

    # 将拼好的图片存储起来
        #New_Image.show()
    New_Image.save(save_path +'weixin.jpg','JPEG')   #指定图片名字以及类型

if __name__ == '__main__':
    file_path = 'E://weixin/'
    save_path = 'E://weixin2/'
    download_headimg(file_path)
    splice_imgs(file_path, save_path)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值