码迷,mamicode.com
首页 > 微信 > 详细

爬微信好友头像

时间:2019-03-11 22:32:25      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:login   line   erro   头像   hot   时间   user   发送   下载   

从其他地方学的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()

爬微信好友头像

标签:login   line   erro   头像   hot   时间   user   发送   下载   

原文地址:https://www.cnblogs.com/pengpengpig/p/10513610.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!