43

itchat 保存好友信息以及生成好友头像图片墙

 4 years ago
source link: https://mp.weixin.qq.com/s/EOKfuloTj_RVr02x7BW_Zg?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

2019 第 41 篇,总第 65 篇文章

本文大约 4000 字,阅读大约需要 12 分钟

最近简单运用 itchat 这个库来实现一些简单的应用,主要包括以下几个应用:

  • 统计保存好友的数量和信息

  • 统计和保存关注的公众号数量和信息

  • 简单生成好友头像的图片墙,利用一个第三方库生成马赛克风格的图片墙

itchat 的 github 项目地址如下,这是一个开源的微信个人接口:

https://github.com/littlecodersh/ItChat

这个库的安装也很简单,直接用 pip install itchat 即可安装

接下来就开始介绍如何利用这个库来实现上述操作。

1. 统计保存好友的数量和信息

首先是微信登录,简单的几行代码即可实现:

import itchat

# 避免频繁扫描二维码登录
itchat.auto_login(hotReload=True)
itchat.dump_login_status()

运行这段代码后,就会弹出一个二维码,进行扫描登录,其中 hotReload=True 是保证不用每次运行程序都需要弹出二维码扫描登录。

然后是获取好友的信息:

we_friend = itchat.get_friends(update=True)[:]

这里 we_friend 就是保存了好友信息的一个字典,并且  we_friend[0] 是保存用户自己的信息,从 we_friend[1] 开始才是真正的好友的信息,这里我们将主要保存以下信息:

key 含义 NickName 昵称 RemarkName 备注 Sex 性别 Province 省份 City 城市 Signature 签名

保存好友的信息代码如下:

friends = we_friend[1:]
total_numbers = len(friends)
print('你的好友数量为: {}'.format(total_numbers))
friend_infos_dict = {}
for fri_info in friends:
for key in friend_key:
if friend_infos_dict.get(key, False):
friend_infos_dict[key].append(fri_info[key])
else:
friend_infos_dict[key] = [fri_info[key]]
# 保存信息
fri_save_file_name = os.path.join(save_file_path, '好友信息.csv')
df = pd.DataFrame(friend_infos_dict)
df.to_csv(fri_save_file_name, sep=',')

其中 save_file_path 是指定保存好友信息文件的文件夹路径,

2. 保存公众号信息

获取公众号信息并保存的代码如下:

# 公众号获取的信息内容,分别是昵称、城市、城市、签名
mps_key = ['NickName', 'City', 'Province', 'Signature']
# 获取公众号信息
mps = itchat.get_mps(update=True)
mps_num = len(mps)
print('你关注的公众号数量: {}'.format(mps_num))

mps_save_file_name = os.path.join(save_file_path, '公众号信息.csv')
mps_dict = {}
for mp in mps:
for key in mps_key:
if mps_dict.get(key, False):
mps_dict[key].append(mp[key])
else:
mps_dict[key] = [mp[key]]

df = pd.DataFrame(mps_dict)
df.to_csv(mps_save_file_name, sep=',', encoding='utf-8')

3. 生成好友头像图片墙

首先同样需要获取好友的头像,并保存到本地,代码如下:

def save_head_photo(save_photo_dir):
itchat.auto_login(hotReload=True)
itchat.dump_login_status()
friends = itchat.get_friends(update=True)[1:]

# 采集好友头像并保存到本地
num = 0
for fri in friends:
img = itchat.get_head_img(userName=fri['UserName'])
img_path = os.path.join(save_photo_dir, str(num) + '.jpg')
if not os.path.exists(img_path):
file_image = open(img_path, 'wb')
file_image.write(img)
file_image.close()
num += 1

print('完成好友头像保存至路径: ', save_photo_dir)

其中获取头像的函数是 itchat.get_head_image()

接着就是生成好友头像的图片墙,这里有两种方式,第一种是比较常规的生成方法。首先需要导入以下库

import itchat
import math
import PIL.Image as Image
import os

接着是设置画布大小及每行的头像数量,头像的大小,代码是:

 # 画布大小
image_size = 1280
# 算出每张图片的大小多少合适
each_size = int(math.sqrt(float(image_size * image_size) / len(ls)))
# 每行图片数量
lines = int(image_size / each_size)
print('each_size={}, lines={}'.format(each_size, lines))
# 创建 1280*1280 的画布
image = Image.new('RGBA', (image_size, image_size))

利用的是 pillow 库,安装方式是  pip install pillow 。这里我设置的画布大小就是 1280 * 1280。

然后就是读取保存的头像,并逐一粘贴到画布上,代码如下:

# 读取保存的好友头像图片
ls = os.listdir(save_photo_dir)
for i in range(0, len(ls)):
try:
img_path = os.path.join(save_photo_dir, str(i) + ".jpg")
img = Image.open(img_path)
except IOError:
print("Error for image: {}".format(img_path))
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(os.path.join(os.getcwd(), "好友头像拼接图.jpg"))

第二种是参考了 当 Python 遇上你的微信好友 介绍的第三方库  photomosaic ,安装方法也很简单:

pip install photomosaic

这个第三方库可以生成蒙太奇马赛克风格的图片或者视频。

实现代码如下:

import photomosaic as pm

def create_photomosaic(save_photo_dir, background_photo):
# 读取背景图片
bg_photo = pm.imread(background_photo)
# 读取好友头像图片,定义图片库
pool = pm.make_pool(os.path.join(save_photo_dir, '*.jpg'))
# 制作 50*50 的拼图马赛克
image = pm.basic_mosaic(bg_photo, pool, (50, 50))
# 保存结果
pm.imsave('马赛克好友头像图片.jpg', image)

其中上述的四行代码也是最基本的使用代码,包括:

  • 选择背景图片

  • 定义图片库

  • 制作马赛克拼图

  • 保存图片

这里我简单选择了下面这张背景图片:

fE7RviE.jpg!web

生成结果如下:

qAnIZry.jpg!web

小结

简单运用 itchat 实现了以上三个小应用,实际上还可以有更多的应用,比如再根据好友信息分析性别比例、好友区域分布、签名的情感分析、关注的公众号类别、给特定的好友发送信息,以及制作微信机器人等。

本文的代码已经上传到 github 上:

https://github.com/ccc013/Python_Notes/tree/master/Projects/wechatProjects/itchat

也可以按如下操作获取代码:

1.关注公众号“ 机器学习与计算机视觉

2.在公众号后台回复“

itchat

",即可获取代码

参考:

欢迎关注我的微信公众号--机器学习与计算机视觉,或者扫描下方的二维码,大家一起交流,学习和进步!

eAnyEvi.jpg!web

往期精彩推荐

机器学习系列

Github项目 & 资源教程推荐


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK