python 獲取所有微信好友頭像圖片,並合並


#-*- coding:utf-8 -*-

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

# 給auto_login方法傳入值為真的hotReload.即使程序關閉,一定時間內重新開啟也可以不用重新掃碼
itchat.auto_login(hotReload=True)
friends = itchat.get_friends(update=True)

# 下載所有好友的頭像圖片
num = 0
for i in friends:
    img = itchat.get_head_img(i["UserName"])
    with open('./headImg/' + str(num) + ".jpg", 'wb') as f:
        f.write(img)
        f.close()
        num += 1
# 獲取文件夾內的文件個數
length = len(os.listdir('./headImg'))
# 根據總面積求每一個的大小
each_size = int(math.sqrt(float(810 * 810) / length))
# 每一行可以放多少個
lines = int(810 / each_size)
# 生成白色背景新圖片
image = Image.new('RGBA', (810, 810), 'white')
x = 0
y = 0
for i in range(0, length):
    try:
        img = Image.open('./headImg/' + str(i) + ".jpg")
    except IOError:
        print(i)
        print("Error")
    else:
        img = img.resize((each_size, each_size), Image.ANTIALIAS)  # resize image with high-quality
        image.paste(img, (x * each_size, y * each_size))
        x += 1
        if x == lines:
            x = 0
            y += 1
image.save('./headImg/' + "all.jpg")
# 通過文件傳輸助手發送到自己微信中
itchat.send_image('./headImg/' + "all.jpg", 'filehelper')
image.show()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM