Python通过ItChat获取朋友图像生成拼接图

参考
https://github.com/littlecodersh/ItChat
https://blog.csdn.net/tangyang8941/article/details/82837284

简介
itchat是一个开源的微信个人号接口,使用python调用微信从未如此简单。使用不到三十行的代码,你就可以完成一个能够处理所有信息的微信机器人。当然,该api的使用远不止一个机器人,更多的功能等着你来发现

安装

pip install itchat

获取朋友圈图片

import itchat
import os


# 获取数据
def get_image():
    itchat.auto_login()
    friends = itchat.get_friends(update=True)

    #  在当前位置创建一个用于存储头像的目录headImages
    base_path = 'headImages'
    if not os.path.exists(base_path):
        os.mkdir(base_path)

    # 获取所有好友头像
    for friend in friends:
        img_data = itchat.get_head_img(userName=friend['UserName'])  # 获取头像数据
        img_name = friend['RemarkName'] if friend['RemarkName'] != '' else friend['NickName']
        img_file = os.path.join(base_path, img_name + '.jpg')
        print(img_file)
        with open(img_file, 'wb') as file:
            file.write(img_data)

if __name__ == '__main__':
    get_image()

拼接朋友图像图片
代码如下

import os
import math
import  itchat
from PIL import Image

# 拼接头像
def join_image():
    base_path = 'headImages'
    files = os.listdir(base_path)
    each_size = int(math.sqrt(float(750 * 750) / len(files)))
    lines = int(750 / each_size)
    image = Image.new('RGB', (750, 750))
    x = 0
    y = 0
    for file_name in files:
        img = Image.open(os.path.join(base_path, file_name))
        img = img.resize((each_size, each_size), Image.ANTIALIAS)
        image.paste(img, (x * each_size, y * each_size))
        x += 1
        if x == lines:
            x = 0
            y += 1
    image.save('all.jpg')


join_image()

拼接图如下