1. 程式人生 > >Python實現好友全頭像的拼接

Python實現好友全頭像的拼接

open 文件 variable ble ges each gin rgba dea

技術分享
微信好友全頭像

話不多說,直接上代碼

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官方介紹

代碼運行過程中會出現登錄二維碼,用微信掃一下,你就可以看到處理的進度。一會你的微信文件傳輸助手就會收到拼接好的頭像圖片。

學習過程中遇到什麽問題或者想獲取學習資源的話,歡迎加入學習交流群
626062078,我們一起學Python!

Python實現好友全頭像的拼接