Python | 生成微信好友图

2018-10-16  本文已影响0人  T_K_233

获取微信头像

用 itchat 登陆微信,并用 get_head_img 来获取头像图片

'''
extract_img.py
get images from WeChat friends list.
'''
import itchat

itchat.auto_login(hotReload=True)

friends = itchat.get_friends(update=True)

for i, friend in enumerate(friends):
    itchat.get_head_img(userName=friend['UserName'], picDir='img/%d.png' % i)

拼图

'''
make_image.py
read all the images and combine into a single one. 
'''
import os
import numpy as np
import cv2


files = list(os.walk('img'))[0][-1]

img_size = (100, 100)
result_grid = (35, 14)

for i in range(result_grid[1]):
    try:
        img_f = cv2.imread('img/'+files[i * result_grid[0]])
        img_f = cv2.resize(img_f, img_size, interpolation=cv2.INTER_CUBIC)
    except:
        img_f = np.zeros((img_size[0], img_size[1], 3), dtype=np.uint8)
    for j in range(1, result_grid[0]):

        try:
            img_t = cv2.imread('img/'+files[i * result_grid[0] + j])
            img_t = cv2.resize(img_t, img_size, interpolation=cv2.INTER_CUBIC)
        except:
            img_t = np.zeros((img_size[0], img_size[1], 3), dtype=np.uint8)
        img_f = np.append(img_f, img_t, axis=1)
    if i == 0:
        img = img_f
    else:
        img = np.append(img, img_f, axis=0)
cv2.imwrite('result.png', img)

不是最优的方法,但是能用就行~

结果

找找有没有你认识的人~
上一篇下一篇

猜你喜欢

热点阅读