用python itchat包 爬取微信好友头像形成矩形头像集
2018-01-24 11:18
1161 查看
原创作品,转载请注明作者 abysscarry-袁杰丶
大家可以发朋友圈开启辨认大赛哈哈~
话不多说,直接上代码,注释我写了比较多,大家应该能看懂
运行结果:
初学python,我们必须干点有意思的事!从微信下手吧!
头像集样例如下:大家可以发朋友圈开启辨认大赛哈哈~
话不多说,直接上代码,注释我写了比较多,大家应该能看懂
import itchat import os import PIL.Image as Image from os import listdir import math import sys print("请输入查询模式:0-显示所有好友头像,但最终矩形头像集最后一行可能残缺;1-头像集为完整矩形,但好友可能不全,即在0模式下舍弃最后一行") mode = input() if mode not in ("0","1"): print("请按照正确格式输入!") sys.exit(0) # itchat.auto_login(enableCmdQR=True) # 这种登录时控制台生成登录二维码 itchat.login() # 这种登录是生成二维码图片在本地目录 friends = itchat.get_friends(update=True)[0:] # 核心:得到frieds列表集,内含很多信息 user = friends[0]["UserName"] w = open(user+"_friends",'a',encoding='utf-8',errors='ignore') # 将friends列表存下来,看看内容 for i in friends: w.write(str(i)) 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) # 得到user目录下的所有文件,即各个好友头像 numPic = len(pics) print("所有好友头像数:"+ str(numPic)) eachsize = int(math.sqrt(float(640 * 640) / numPic)) # 先圈定每个正方形小头像的边长,如果嫌小可以加大 print("小正方形头像边长:"+ str(eachsize)) numrow = int(640 / eachsize) print("一行小头像数:"+ str(numrow)) if mode == "0": numcol = int(math.ceil(numPic * 1.0 / numrow)) # 向上取整 else: numcol = int(numPic / numrow) # 向下取整 print("舍弃好友数:"+ str(numPic - numrow * numcol)) toImage = Image.new('RGB', (eachsize*numrow, eachsize*numcol)) # 先生成头像集模板 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 == numrow: x = 0 y += 1 toImage.save(user + ".jpg") # itchat.send_image(user + ".jpg", 'filehelper') # 自动向文件助手里面添加图片,不需要可以关闭
运行结果:
ok!!!
相关文章推荐
- 使用python抓取微信好友个性签名并形成词云(itchat;wordcloud)
- Python微信好友信息意义及取值分析(基于itchat)
- 使用Python微信库itchat获得好友和群组已撤回的消息
- Python利用itchat对微信中好友数据实现简单分析的方法
- itchat+pillow实现微信好友头像爬取和拼接
- Python实现微信好友签名词云的构建(itchat、jieba、wordcloud)
- Python实践-itchat获取微信好友总人数以及男女比例
- Python之itchat生成微信头像拼接问题
- 使用Python itchat个人微信账号头像拼接
- Python微信库:itchat的用法详解
- Python学习-Itchat微信API
- python使用itchat发送微信消息提醒
- python实现微信接口(itchat)
- 有了 itchat, python 调用微信个人号从未如此简单(新增 py3 支持)
- python-itchat 统计微信群、好友数量,及原始消息数据
- 【itchat】用Python玩耍微信
- python实现微信接口(itchat)
- python获得微信好友头像
- python微信模块 itchat
- 用python中的itchat框架 筛选出 指定某个微信群中 是否还有你自己的好友 并列出来