您的位置:首页 > 编程语言 > Python开发

Python拼接微信好友头像大图的实现方法

2018-08-01 08:22 1006 查看

基于 itchat 库来获取微信好友头像并执行拼接操作,对微信上文字化好友列表数据进行可视化展示。

获取好友头像

def save_avatar(folder):
"""
保存微信好友头像
:param folder: 保存的文件夹
"""
itchat.auto_login(hotReload=True)
users = itchat.get_friends() or []
print('%d friends found.' % len(users))
if not os.path.exists(folder):
os.makedirs(folder)
index = 1
for i, user in enumerate(users):
nickname = user.RemarkName
username = user.UserName
file_path = os.path.join(folder, '%03d_%s.png' % (i, nickname))
if not os.path.isfile(file_path): # 不重复下载
avatar = itchat.get_head_img(username)
with open(file_path, 'w') as f:
f.write(avatar)
print('Download %d: %s' % (index, file_path))
index += 1

这里只需要传入一个保存头像的文件夹即可,运行 itchat.auto_login(hotReload=True) 后会弹出微信扫码界面让你授权微信登录,以便接下来的好友数据获取。

在图片下载时,我添加了一个防止重复下载的判断,以免多次运行时每次都要重新进行头像的下载。

取出待拼接头像

def get_image_files(folder, filters=None):
"""
取出待拼接头像
:param folder: 目标文件夹
:param filters: 需要过滤的图片
:return: 头像路径
"""
filters = filters or []
filenames = [os.path.join(folder, sub) for sub in os.listdir(folder)
if sub.endswith('.png') and not filters.__contains__(sub)]
return filenames

这里单独写个方法是为了把过滤的逻辑封装进来,以便于去掉指定的微信好友的头像(比如纯色的头像在拼接之后的大图看上去很明显,非强迫症可忽略)。

计算拼接的排列

def calculate_align_way(image_num, force_align=False):
"""
计算图片排版对齐方式
:param image_num: 图片数量
:return: (rowls, columns)
"""
actual_value = image_num ** 0.5
suggest_value = int(actual_value)
if actual_value == suggest_value or force_align:
return suggest_value, suggest_value
else:
return suggest_value, suggest_value + 1

因为需要知道最终拼接图片的行列数,所有这里单独定义一个计算方法。算法就是直接对图片总数开根号,取出的结果如果正好是整数,就直接返回该结果。如果不是整数(大多数情况都如此),则根据参数 force_align 来决定是否强制进行正好全部铺满的显示。如果设为 True ,能强制铺满,但会有部分好友未显示完全;反之则是相对的情况。 后面发现拼接图片最后一行有很多黑色空位时,只需要更改该参数为True即可。

拼接

def join_images(image_files, rows, cols, width, height, save_file=None):
"""
拼接操作
:param image_files: 待拼接的图片
:param rows: 行数
:param cols: 列数
:param width: 每张小头像的宽度
:param height: 每张小头像的高度
:param save_file: 拼接好图片的保存路径
"""
canvas = np.ones((height * rows, width * cols, 3), np.uint8)
for row in range(rows):
for col in range(cols):
index = row * cols + col
if index >= len(image_files):
break
file_path = image_files[index]
im = Image.open(file_path)
im = im.resize((width, height))
im_data = np.array(im)
if len(im_data.shape) == 2:
im_data = np.expand_dims(im_data, -1)
x = col * width
y = row * height
canvas[y: y + height, x: x + width, :] = im_data
image = Image.fromarray(canvas)
image.show()
if save_file:
image.save(save_file)

拼接图片调用的是科学计算包 numpy 和图片库 PIL ,主要就是对 ndarray 进行操作。

最终将上面的步骤全部串联起来,执行如下主函数,便得到上面的拼接图片。

FOLDER = 'avatars'
if __name__ == '__main__':
# 保存所有好友头像
save_avatar(FOLDER)
# 取到准备拼接的头像
image_files = get_image_files(FOLDER)
# 计算拼接的行列
rows, columns = calculate_align_way(len(image_files), force_align=True)
# 执行拼接操作
join_images(image_files, rows, columns, 64, 64, 'result.png')

Github源码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 微信 头像 拼接