爬微信好友头像

Posted pengpengpig

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了爬微信好友头像相关的知识,希望对你有一定的参考价值。

从其他地方学的2个爬微信好友头像的方法,分享下,侵删。
#coding:utf-8
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(‘user‘ + "/" + str(num) + ".jpg")
fileImage.write(img)
fileImage.close()
num += 1

# 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



ls = os.listdir(‘user‘)
each_size = int(math.sqrt(float(640*640)/len(ls)))
lines = int(640/each_size)
image = Image.new(‘RGB‘, (640, 640))
x = 0
y = 0
for i in range(0,len(ls)+1):
try:
img = Image.open(‘user‘ + "/" + 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(‘user‘ + "/" + "all.jpg")
itchat.send_image(‘user‘ + "/" + "all.jpg", ‘filehelper‘)

===============================

#coding:utf-8
import itchat
import math
import os
import PIL.Image as Image

#给auto_login方法传入值为真的hotReload.即使程序关闭,一定时间内重新开启也可以不用重新扫码
itchat.auto_login(hotReload=True)
friends = itchat.get_friends(update=True)

#下载所有好友的头像图片
num = 0
for i in friends:
img = itchat.get_head_img(i["UserName"])
with open(‘./headImg/‘ + str(num) + ".jpg",‘wb‘) as f:
f.write(img)
f.close()
num += 1
#获取文件夹内的文件个数
length = len(os.listdir(‘./headImg‘))
#根据总面积求每一个的大小
each_size = int(math.sqrt(float(810*810)/length))
#每一行可以放多少个
lines = int(810/each_size)
#生成白色背景新图片
image = Image.new(‘RGBA‘, (810, 810),‘white‘)
x = 0
y = 0
for i in range(0,length):
try:
img = Image.open(‘./headImg/‘ + str(i) + ".jpg")
except IOError:
print(i)
print("Error")
else:
img = img.resize((each_size, each_size), Image.ANTIALIAS) #resize image with high-quality
image.paste(img, (x * each_size, y * each_size))
x += 1
if x == lines:
x = 0
y += 1
image.save(‘./headImg/‘ + "all.jpg")
#通过文件传输助手发送到自己微信中
itchat.send_image(‘./headImg/‘ + "all.jpg",‘filehelper‘)
image.show()

以上是关于爬微信好友头像的主要内容,如果未能解决你的问题,请参考以下文章

android 实现类似微信缓存和即时更新好友头像

Python微信好友头像拼接图

Python3 生成微信好友头像的图片合集

对方头像更新了但我看不到怎么回事?

itchat库初探--微信好友全头像的拼接

PHP--仿微信, 通过登陆者用户名显示好友列表,显示头像和昵称