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

yipeiwu_com6年前Python基础

基于 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设计】。

相关文章

Python读写docx文件的方法

Python读写word文档有现成的库可以处理。我这里采用 python-docx。可以用pip install python-docx安装一下。 这里说一句,ppt和excel也有类似...

用matplotlib画等高线图详解

用matplotlib画等高线图详解

等高线图是在地理课中讲述山峰山谷时绘制的图形,在机器学习中也会被用在绘制梯度下降算法的图形中。 因为等高线的图有三个信息:x,y以及x,y所对应的高度值。 这个高度值的计算我们用一个函数...

使用APScheduler3.0.1 实现定时任务的方法

需求是在某一指定的时刻执行操作 网上的建议多为通过调用Scheduler的add_date_job实现 不过APScheduler 3.0.1与之前差异较大, 无法通过上述方法实现 参考...

python 整数越界问题详解

python 内部自带大整数运算能力,整数运算不会溢出,只要内存足够,就oK 下面的例子演示了两个32位整数加法的情况(通过位运算实现),为了模拟溢出的效果,必须人工的进行位运算,~运算...

将tensorflow.Variable中的某些元素取出组成一个新的矩阵示例

在神经网络计算过程中,经常会遇到需要将矩阵中的某些元素取出并且单独进行计算的步骤(例如MLE,Attention等操作)。那么在 tensorflow 的 Variable 类型中如何做...