使用python itchat包爬取微信好友头像形成矩形头像集的方法

作者:袁杰丶 时间:2021-02-01 17:54:10 

初学python,我们必须干点有意思的事!从微信下手吧!

头像集样例如下:

使用python itchat包爬取微信好友头像形成矩形头像集的方法

大家可以发朋友圈开启辨认大赛哈哈~

话不多说,直接上代码,注释我写了比较多,大家应该能看懂


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')  # 自动向文件助手里面添加图片,不需要可以关闭

运行结果:

使用python itchat包爬取微信好友头像形成矩形头像集的方法

ok!!!

来源:https://blog.csdn.net/Abysscarry/article/details/79148584

标签:python,itchat,头像
0
投稿

猜你喜欢

  • php中实现记住密码自动登录的代码

    2023-11-14 18:36:14
  • PHP商品秒杀问题解决方案实例详解【mysql与redis】

    2023-09-27 10:49:04
  • 安装了Office2003补丁之后,access不能用,打不开了

    2011-05-12 12:19:00
  • 编写SQL需要注意的细节Checklist总结

    2012-10-07 10:43:57
  • 如何应对SQL Server数据库崩溃

    2008-11-24 17:25:00
  • Python2与Python3的区别实例分析

    2021-01-07 11:47:17
  • ASP可显示和隐藏的树型菜单

    2007-10-01 14:40:00
  • MySQL乱码问题深层分析

    2009-03-09 14:53:00
  • CSS自适应宽度圆角按钮

    2007-11-20 11:38:00
  • python循环输出三角形图案的例子

    2022-05-16 08:38:37
  • 详谈python在windows中的文件路径问题

    2023-12-16 22:02:34
  • python选择排序算法实例总结

    2023-08-29 06:58:28
  • ASP和MYSQL开发网站的注意事项

    2009-08-21 13:23:00
  • Python爬虫小技巧之伪造随机的User-Agent

    2023-11-02 21:32:41
  • 利用Python实现Windows下的鼠标键盘模拟的实例代码

    2023-06-22 04:37:31
  • 如何使用Python进行OCR识别图片中的文字

    2021-05-05 13:11:07
  • 一文教你利用Python画花样图

    2023-09-01 20:26:42
  • Python 从attribute到property详解

    2022-03-15 18:52:40
  • 如何在sublime编辑器中安装python

    2021-01-12 05:27:00
  • 懒懒交流会:ClassName的长命名 VS. 短命名

    2009-11-28 16:08:00
  • asp之家 网络编程 m.aspxhome.com