一键拼接微信好友头像/玩炫朋友圈
在这里提供一键拼接微信好友头像的源码,分享到朋友赚足眼球。
另附打包好的工具,下载即用!
import sys
from wxpy import *
import math
from PIL import Image
import os
import time
# 创建头像存放文件夹
def creat_filepath():
avatar_dir = os.getcwd() + "\\wechat\\"
if not os.path.exists(avatar_dir):
os.mkdir(avatar_dir)
return avatar_dir
# 保存好友头像
def save_avatar(avatar_dir):
# 初始化机器人,扫码登陆
print('扫描二维码,安全登录...\n\n')
bot = Bot()
friends = bot.friends(update=True)
print('\n\n登录成功,开始获取好友头像...\n\n')
total = len(friends)
bar = 50
num = 0
for friend in friends:
friend.get_avatar(avatar_dir + '\\' + str(num) + ".jpg")
hashs = '#' * int(float(num)/ float(total) * bar)
strpercent = (str(float(num)/float(total) * 100.0))[0:4] + '%'
if(num == total):
sys.stdout.write("\r进度:%s 完成! " % hashs)
time.sleep(0.5)
else:
sys.stdout.write("\r进度:%s .../%s" % (hashs, strpercent))
sys.stdout.flush()
num = num + 1
if(num == total):
sys.stdout.write("\r进度:%s 完成!" % hashs)
# 拼接头像
def joint_avatar(path):
# 获取文件夹内头像个数
length = len(os.listdir(path))
# 设置画布大小
#image_size = 2560
image_size = 6250
# 设置每个头像大小
each_size = math.ceil(2560 / math.floor(math.sqrt(length)))
# 计算所需各行列的头像数量
x_lines = math.ceil(math.sqrt(length))
y_lines = math.ceil(math.sqrt(length))
image = Image.new('RGB', (each_size * x_lines, each_size * y_lines))
x = 0
y = 0
for (root, dirs, files) in os.walk(path):
for pic_name in files:
# 增加头像读取不出来的异常处理
try:
with Image.open(path + pic_name) as img:
img = img.resize((each_size, each_size))
image.paste(img, (x * each_size, y * each_size))
x += 1
if x == x_lines:
x = 0
y += 1
except IOError:
print('\n')
#print("头像读取失败")
img = image.save(os.getcwd() + "/wechat.png")
print('合成图片为当前目录的wechat.png\n\n')
print('微信好友头像拼接完成!\n\n')
print('感谢您的使用,再会!\n\n')
time.sleep(10.0)
if __name__ == '__main__':
print('******************声明******************\n\n')
print('本程序源码来自于网络,本程序可用来学习、娱乐及交流,不可用于任何商业行为。\n\n')
print('程序开始运行,请稍候...\n\n')
avatar_dir = creat_filepath()
save_avatar(avatar_dir)
joint_avatar(avatar_dir)
工具下载链接:
关注本公众号,后台回复【一键拼接微信好友头像】,即可获取下载链接。