itchat库初探--微信好友全头像的拼接

all.jpg

代码:

import itchat
import math
import PIL.Image as Image
import os

itchat.auto_login()
friends = itchat.get_friends(update=True)[0:]
user = friends[0]["UserName"]

num = 0
for i in friends:
    img = itchat.get_head_img(userName=i["UserName"])
    fileImage = open(r'文件夹' + "/" + str(num) + ".jpg",'wb')
    fileImage.write(img)
    fileImage.close()
    num += 1

ls = os.listdir(r'文件夹')
each_size = int(math.sqrt(float(640*640)/len(ls)))
lines = int(640/each_size)
image = Image.new('RGBA', (640, 640))
x = 0
y = 0
for i in range(0,len(ls)+1):
    try:
        img = Image.open(r'文件夹' + "/" + str(i) + ".jpg")
    except IOError:
        print("Error")
    else:
        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(r'文件夹' + "/" + "all.jpg")
itchat.send_image(r'文件夹' + "/" + "all.jpg", 'filehelper')

代码中的文件夹”是需要创建一个文件夹,并用该文件夹的路径替换调文件夹

代码运行需要安装两个库

pip install itchat
pip install pillow

如果安装python的时候pip安装选项没打√ ,就先安装pip。Python和pip的安装

itchat官方介绍

代码运行过程中会出现登录二维码,用微信扫一下,你就可以看到处理的进度。稍后你的微信文件传输助手就会收到拼接好的头像图片。

猜你喜欢

转载自blog.csdn.net/xc_zhou/article/details/80557078