微信好友全頭像
話不多說,直接上代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
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官方介紹
代碼運行過程中會出現登錄二維碼,用微信掃一下,你就可以看到處理的進度。一會你的微信文件傳輸助手就會收到拼接好的頭像圖片。
以上這篇Python實現好友全頭像的拼接實例(推薦)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。