itchat+pillow實現微信好友頭像爬取和拼接


本項目github地址


效果圖

demo1
demo1
demo2
demo2
demo3
demo3
demo4
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')




最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,284評論 25 708
  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,232評論 4 61
  • 時間:2016.3.13 進度:68-71 感悟:我們可以先建立了社會主義制度,卻可以搞不清什么是社會主義本質。這...
    24K超超老師閱讀 151評論 0 0
  • 回想起來,大寶好幾次發燒、咳嗽都是在我給他喝了比較多配方牛奶粉之后。按照李辛醫生的說法,幾乎可以肯定是跟牛奶有關。...
    星流云水閱讀 397評論 2 1
  • 也許上了年紀,變得更加思鄉。上海的夏與故鄉的夏大相徑庭。這里是車水馬龍,燈紅酒綠,人頭攢動,故鄉是蛙鳴蟲唱,月光如...
    深情無用閱讀 336評論 0 0