微信好友全頭像
話不多說,直接上代碼
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('文件夾' + "/" + str(num) + ".jpg",'wb')
fileImage.write(img)
fileImage.close()
num += 1
ls = os.listdir('文件夾')
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('文件夾' + "/" + 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('文件夾' + "/" + "all.jpg")
itchat.send_image('文件夾' + "/" + "all.jpg", 'filehelper')
代碼運行需要安裝兩個庫
pip install itchat
pip install pillow
如果安裝python的時候pip
安裝選項沒打√ 殴俱,就先安裝pip
。
Python和pip的安裝
itchat官方介紹
**代碼運行過程中會出現(xiàn)登錄二維碼枚抵,用微信掃一下线欲,你就可以看到處理的進度。稍后你的微信文件傳輸助手就會收到拼接好的頭像圖片汽摹。
如果你覺得我的文章還可以李丰,可以關(guān)注我的微信公眾號:Python攻城獅
可掃描二維碼,添加關(guān)注