本項目github地址
效果圖

demo1

demo2

demo3

demo4
使用方法(前提是設備安裝了python):
下載本項目到本地,打開項目主目錄,打開命令行,輸入:
pip install -r requirements.txt
等待安裝完成,輸入:
python wxImage.py
出現如下二維碼:

二維碼
用手機微信右上角的掃一掃,確認登陸即可。
稍等片刻,你打開手機微信,找到信息欄的微信傳輸助手,會看到如下:

微信文件傳輸助手
核心
python:
- itchat(用于爬取頭像)
- pillow(用于拼接圖片)
源碼詳解
首先登陸python版本微信itchat,生成二維碼:
itchat.auto_login(enableCmdQR=True)
獲取好友列表:
friends = itchat.get_friends(update=True)[0:]
然后使用itchat的get_head_img(userName=none)函數來爬取好友列表的頭像,并下載到本地:
num = 0
for i in friends:
img = itchat.get_head_img(userName=i["UserName"])
fileImage = open(user + "/" + str(num) + ".jpg",'wb')
fileImage.write(img)
fileImage.close()
num += 1
計算出每張頭像縮小后的尺寸(由于為了拼接之后可以用來作為為微信頭像,所以合成的圖片大小都是640 * 640的,因為微信頭像大小就是640 * 640)
計算每張頭像縮小后的邊長(默認為正方形):
eachsize = int(math.sqrt(float(640 * 640) / numPic))
計算合成圖片每一邊分為多少小邊:
numline = int(640 / eachsize)
縮小并拼接圖片:
x = 0
y = 0
for i in pics:
try:
#打開圖片
img = Image.open(user + "/" + i)
except IOError:
print("Error: 沒有找到文件或讀取文件失敗")
else:
#縮小圖片
img = img.resize((eachsize, eachsize), Image.ANTIALIAS)
#拼接圖片
toImage.paste(img, (x * eachsize, y * eachsize))
x += 1
if x == numline:
x = 0
y += 1
保存圖片到本地:
toImage.save(user + ".jpg")
在微信的文件傳輸助手發合成后的圖片給使用者:
itchat.send_image(user + ".jpg", 'filehelper')
完整代碼(下載本人github項目會更好點):
from numpy import *
import itchat
import urllib
import requests
import os
import PIL.Image as Image
from os import listdir
import math
itchat.auto_login(enableCmdQR=True)
friends = itchat.get_friends(update=True)[0:]
user = friends[0]["UserName"]
print(user)
os.mkdir(user)
num = 0
for i in friends:
img = itchat.get_head_img(userName=i["UserName"])
fileImage = open(user + "/" + str(num) + ".jpg",'wb')
fileImage.write(img)
fileImage.close()
num += 1
pics = listdir(user)
numPic = len(pics)
print(numPic)
eachsize = int(math.sqrt(float(640 * 640) / numPic))
print(eachsize)
numline = int(640 / eachsize)
toImage = Image.new('RGBA', (640, 640))
print(numline)
x = 0
y = 0
for i in pics:
try:
#打開圖片
img = Image.open(user + "/" + i)
except IOError:
print("Error: 沒有找到文件或讀取文件失敗")
else:
#縮小圖片
img = img.resize((eachsize, eachsize), Image.ANTIALIAS)
#拼接圖片
toImage.paste(img, (x * eachsize, y * eachsize))
x += 1
if x == numline:
x = 0
y += 1
toImage.save(user + ".jpg")
itchat.send_image(user + ".jpg", 'filehelper')