有趣的事,Python永远不会缺席!
如需转发,请注明出处:小婷儿的python https://www.cnblogs.com/xxtalhr/p/10859517.html一、制作微信头像墙(方形)
1 # coding: utf-8 2 from wxpy import Bot, Chat 3 4 import math 5 import os 6 from PIL import Image 7 8 9 class WxFriendImage(Chat): 10 @staticmethod 11 def get_image(): 12 path = os.path.abspath('.') 13 bot = Bot() # 机器人对象 14 friends = bot.friends(update=True) 15 16 dirs = path + '\\wx-tx' 17 if not os.path.exists(dirs): 18 os.mkdir('wx-tx') 19 20 index = 0 21 for friend in friends: 22 print(f'正在保存{friend.nick_name}的微信头像') 23 friend.get_avatar(dirs + '\\' + f'{str(index)}.jpg') 24 index += 1 25 26 return dirs 27 28 @staticmethod 29 def composite_image(dirs): 30 images_list = os.listdir(dirs) 31 images_list.sort(key=lambda x: int(x[:-4])) # 根据头像名称排序 32 length = len(images_list) # 头像总数 33 image_size = 2560 34 # 每个头像大小 35 each_size = math.ceil(image_size / math.floor(math.sqrt(length))) 36 lines = math.ceil(math.sqrt(length)) # 列数 37 rows = math.ceil(math.sqrt(length)) # 行数 38 image = Image.new('RGB', (each_size * lines, each_size * rows)) 39 row = 0 40 line = 0 41 os.chdir(dirs) 42 for file in images_list: 43 try: 44 with Image.open(file) as img: 45 img = img.resize((each_size, each_size)) 46 image.paste(img, (line * each_size, row * each_size)) 47 line += 1 48 if line == lines: 49 line = 0 50 row += 1 51 except IOError: 52 print(f'头像{file}异常,请查看') 53 continue 54 img = image.save(os.getcwd() + '/wx-txq.png') 55 if not img: 56 print('所有的微信头像已合成微信好友照片墙,请查阅wx-txq.png!') 57 58 59 def main(): 60 dirs = WxFriendImage.get_image() 61 WxFriendImage.composite_image(dirs) 62 63 64 if __name__ == '__main__': 65 main()
二、制作微信头像墙(圆形)
结果
Successfully !!!
有趣的事,Python永远不会缺席!还不来加我,瞅什么Ne。哒哒哒。。。