从其他地方学的2个爬微信好友头像的方法,分享下,侵删。
#coding:utf-8import itchatimport mathimport PIL.Image as Imageimport os itchat.auto_login()friends = itchat.get_friends(update=True)[0:]user = friends[0]["UserName"] num = 0for 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 = 0y = 0for 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 += 1image.save(‘user‘ + "/" + "all.jpg")itchat.send_image(‘user‘ + "/" + "all.jpg", ‘filehelper‘) ===============================
#coding:utf-8import itchatimport mathimport osimport PIL.Image as Image #给auto_login方法传入值为真的hotReload.即使程序关闭,一定时间内重新开启也可以不用重新扫码itchat.auto_login(hotReload=True)friends = itchat.get_friends(update=True) #下载所有好友的头像图片num = 0for 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 = 0y = 0for 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 += 1image.save(‘./headImg/‘ + "all.jpg")#通过文件传输助手发送到自己微信中itchat.send_image(‘./headImg/‘ + "all.jpg",‘filehelper‘)image.show()
原文地址:https://www.cnblogs.com/pengpengpig/p/10513610.html
时间: 2024-10-04 01:12:41