前聊天刷朋友圈,晒了一张下面的照片,微信好友墙,一张大图片,展示了自己所有好友的照片!
效果图如下,出于隐私,这里作了模糊处理。
操作系统:Windows
Python版本:3.7.3
我们需要首先需要获取好友的头像信息,接下来处理图像并完成图像的拼接。
在这里,我们登录微信获取好友信息,使用的是wxpy模块,处理并生成最终的图像借助公益诉讼模块。因为都是第三方模块,如环境中没有可以使用脉冲进行安装。另外涉及路径的处理等,我们需要导入操作系统模块和sys模块。
<>之前得到wxpy import * import PIL.Image  as 形象 import 操作系统 import sys
我们要获取微信好友的头,像首先需要登录微信
#,初始化机器人,扫码登陆微信,适用于Windows系统 时间=bot 机器人() #,#,Linux系统,执行登陆请调用下面的这句=#,bot 机器人(console_qr=2,, cache_path=癰otoo.pkl"
在获取好友头像信息之前,我们得先在本地创建一个目录,用于后续存储好友头像的文件。
#,获取当前路径信息 时间=curr_dir get_dir (sys.argv [0]) #,如果FriendImgs目录不存在就创建一个 if not  os.path.exists (curr_dir +,“FriendImgs/?: ,os.mkdir (curr_dir +,“FriendImgs/?
接下来就是获取友头像信息,并将其存储在本地创建的目录中。
时间=<>以前my_friends bot.friends(更新=True) #,获取好友头像信息并存储在FriendImgs目录中 n =0 for friend  my_friends拷贝: ,friend.get_avatar (curr_dir +,“FriendImgs/? +, str (n), +,“.jpg")=,,n  n +, 1这时你就可以在本地FriendImgs文件夹中,看到保存下来的微信好友头像的图片。
接下来,我们需要逐个打开微信好友的图片,使用Image.open()方法。
时间=<>以前img Image.open(时间+ curr_dir “FriendImgs/? +, file_names)将微信头像图片,重置为50 * 50像素尺寸的小图,使用img.resize()方法。
时间=<>以前img img.resize ((50, 50), Image.ANTIALIAS)然后将图片黏贴到我们的照片墙中,使用image.paste()方法。
image.paste (img, (x *, 50岁,y *, 50))
最后将制作完成的照片墙保存下来,使用image.save()方法。
时间=<>以前img image.save(时间+ curr_dir “WeChat_Friends.jpg")现在我们将本小节中代码整合到一起,如下所示:
#,准备生成微信好友头像墙的尺寸 时间=image Image.new (“RGB",, (650,, 650)) #,定义初始图片的位置 x =0 y =0 #,获取下载的头像文件 时间=curr_dir get_dir (sys.argv [0]) 时间=ls os.listdir (curr_dir +, & # 39; FriendImgs& # 39;) #,遍历文件夹的图片 for file_names  ls拷贝: ,试一试: ,#依次打开图片=,,img  Image.open(时间+ curr_dir “FriendImgs/? +, file_names) ,except IOError:那么 ,继续 ,其他的: ,#重新设置图片的大?,,img  img.resize(50岁,50),,Image.ANTIALIAS) ,#将图片粘贴到最终的照片墙上 ,image.paste (img, (x *, 50岁,y *, 50)) ,#设置每一行排13个图像 x +=, 1 ,if x ==, 13: x =, 0 y +=, 1 #,保存图片为WeChat_Friends.jpg 时间=img image.save(时间+ curr_dir “WeChat_Friends.jpg 代码执行后,最终生成的效果图如下:
(这里展示的图片做了模糊处理)