1. 程式人生 > >Python實現爬取好友頭像拼接成大圖!這不就暴露了我的好友了!

Python實現爬取好友頭像拼接成大圖!這不就暴露了我的好友了!

前言

筆者無意間發現一個有趣的第三方庫itchat,itchat模組是一位叫little

codersh的大神寫的模組,附上大神的github地址,有興趣的朋友可以去嘗試玩一下itchat模組,很有趣的!!!

https://github.com/littlecodersh/ItChat


進群:548377875    即可獲取數十套PDF以及大量的學習教程哦!

依賴環境

Python3.6

Sublime Text 3

window10


整體流程是: 爬取頭像—儲存頭像—剪下圖片—將成果傳送到手機傳輸助手


步驟

1.首先安裝itchat,使用pip安裝頗為簡單

pip install inchat

2.將微信好友頭像爬取下來

from numpy import *
import itchat
import PIL.Image as Image
from os import listdir
def get_imgs():#完成主要的下載頭像的任務
 #使用熱登入(已經登入的程式,再一次執行程式不需要掃描驗證碼),執行這一步就會有二維碼需要掃描登入
 itchat.auto_login(hotReload=True)
 #獲取朋友列表,返回字典型別的資料集,獲取好友的索引數
 friends = itchat.get_friends(update=True)[0:]
 #為圖片命名的變數
 num = 0
 #遍歷好友列表
 for i in friends:
 #獲取好友的頭像
 img = itchat.get_head_img(userName=i["UserName"])
 #在專案檔案的主建立一個user檔案用於放頭像,並寫入對應的圖片名,空白的
 fileImage = open( "./user/" + str(num) + ".jpg",'wb')
 #將獲取到的頭像檔案寫到建立的圖片檔案中
 fileImage.write(img)
 #關閉資源
 fileImage.close()
 num += 1

程式碼講解

匯入我們需要用到的itchat模組,定義一個爬取頭像的函式get_big_img()

使用itchat.auto_login()完成登入,這是會在資料夾中生成一個itchat.pkl的檔案用於登入,下次再登入時不需要掃碼了

使用itchat.get_friends()完成朋友列表的資訊獲取,再迴圈列表用itchat.get_head_img()獲取頭像並儲存到user資料夾中,這裡我們完成就第一步

3.拼接頭像

#製作大的大頭像
def get_big_img():
 #獲取usr資料夾所有檔案的名稱
 pics = listdir("use")
 numPic = len(pics)
 #建立圖片大小
 toImage = Image.new("RGB", (800, 800))
 #用於圖片的位置
 x = 0
 y = 0
 #遍歷user資料夾的圖片
 for i in pics:
 try:
 #依次開啟圖片
 img = Image.open("use/{}".format(i))
 except IOError:
 print("Error: 沒有找到檔案或讀取檔案失敗",i)
 else:
 #重新設定圖片的大小
 img = img.resize((50, 50), Image.ANTIALIAS)
 #將圖片貼上到最後的大圖片上,需要注意對應的位置
 toImage.paste(img, (x * 50, y * 50))
 #設定每一行排16個影象
 x += 1
 if x == 17:
 x = 0
 y += 1
 #儲存圖片為bigPhoto.jpg
 toImage.save("use/" +"bigPhoto.jpg")
 #將做好圖片傳送東自己的手機上
 itchat.send_image("use/" +"bigPhoto.jpg", 'filehelper')
#定義執行的主函式
def main():
 get_imgs()
 get_big_img()
#執行
if __name__=="__main__":
 main()
  • 頭像獲取下來自然需要拼接,這裡我們使用PIL庫完成,如果沒有安裝的可以使用pip完成安裝
  • 我們使用Image.new()函式新建一張空白圖片,然後把儲存在資料夾中的圖片,使用img.resize()函式進行剪下成50*50大小,再把50*50的圖片貼上到空白圖片上就完成了

4.貼一下原始碼

from numpy import *
import itchat
import PIL.Image as Image
from os import listdir
def get_imgs():#完成主要的下載頭像的任務
 #使用熱登入(已經登入的程式,再一次執行程式不需要掃描驗證碼),執行這一步就會有二維碼需要掃描登入
 itchat.auto_login(hotReload=True)
 #獲取朋友列表,返回字典型別的資料集,獲取好友的索引數
 friends = itchat.get_friends(update=True)[0:]
 #為圖片命名的變數
 num = 0
 #遍歷好友列表
 for i in friends:
 #獲取好友的頭像
 img = itchat.get_head_img(userName=i["UserName"])
 #在專案檔案的主建立一個user檔案用於放頭像,並寫入對應的圖片名,空白的
 fileImage = open( "./user/" + str(num) + ".jpg",'wb')
 #將獲取到的頭像檔案寫到建立的圖片檔案中
 fileImage.write(img)
 #關閉資源
 fileImage.close()
 num += 1
#製作大的大頭像
def get_big_img():
 #獲取usr資料夾所有檔案的名稱
 pics = listdir("use")
 numPic = len(pics)
 #建立圖片大小
 toImage = Image.new("RGB", (800, 800))
 #用於圖片的位置
 x = 0
 y = 0
 #遍歷user資料夾的圖片
 for i in pics:
 try:
 #依次開啟圖片
 img = Image.open("use/{}".format(i))
 except IOError:
 print("Error: 沒有找到檔案或讀取檔案失敗",i)
 else:
 #重新設定圖片的大小
 img = img.resize((50, 50), Image.ANTIALIAS)
 #將圖片貼上到最後的大圖片上,需要注意對應的位置
 toImage.paste(img, (x * 50, y * 50))
 #設定每一行排16個影象
 x += 1
 if x == 17:
 x = 0
 y += 1
 #儲存圖片為bigPhoto.jpg
 toImage.save("use/" +"bigPhoto.jpg")
 #將做好圖片傳送東自己的手機上
 itchat.send_image("use/" +"bigPhoto.jpg", 'filehelper')
#定義執行的主函式
def main():
 get_imgs()
 get_big_img()
#執行
if __name__=="__main__":
 main()

  • 在最後我們編寫主函式main(),再執行函式即可

事實上itchat能完成的事遠遠不止於此,做一個微信機器人出來也不是不可能的,在下一期我們再進行講解

 

 

 

Python實現爬取好友頭像拼接成大圖!這不就暴露了我的好友了!