Skip to content

Instantly share code, notes, and snippets.

@ZJUGuoShuai
Created June 13, 2019 15:03
Show Gist options
  • Select an option

  • Save ZJUGuoShuai/66454af652a43b9050c3f9c37a3d410f to your computer and use it in GitHub Desktop.

Select an option

Save ZJUGuoShuai/66454af652a43b9050c3f9c37a3d410f to your computer and use it in GitHub Desktop.
将微信好友头像拼成一个图片
import itchat
import os
import math
from PIL import Image
itchat.auto_login(hotReload=True) # 扫码登录微信
if not os.path.exists('img'): # 如果同目录没有img目录
os.mkdir('img') # 创建img目录
"""
friends = itchat.get_friends() # 得到好友列表
num = 0
for friend in friends:
img = itchat.get_head_img(userName=friend['UserName']) # 得到每个好友头像
with open('img/' + str(num) + '.png', 'wb') as f: # 按数字顺序在img目录存好友头像
f.write(img)
num += 1
"""
images = os.listdir('img')
each_size = int(math.sqrt((640 * 640) / len(images))) # 定义好友头像像素
line = int(640 / each_size) # 定义一个新图片中头像行数
image = Image.new('RGBA', (640, 640)) # 定义一个640*640新图片
x = 0
y = 0
for i in images:
img = Image.open('img/' + i)
img1 = img.resize((each_size, each_size), Image.ANTIALIAS) # 缩放好友头像
image.paste(img1, (x * each_size, y * each_size))
x += 1
if x == line:
x = 0
y += 1
image.save('img/all.png') # 将缩放后的好友头像粘到640*640的新图片中
print('Done')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment