python朋友圈头像_用python一键生成头像墙,将你微信好友头像全部收集起来

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理

以下文章来源于腾讯云 作者:Python编程与实战

( 想要学习Python?Python学习交流群:1039649593,满足你的需求,资料都已经上传群文件流,可以自行下载!还有海量最新2020python学习资料。 )

前言

用 python 代码写了一个一键合成微信好友头像墙的程序,效果如下:

不会写代码?没关系!只要你会使用电脑就 ok!

因为除了用代码方式生成外,还建了一个 .exe 的程序,在电脑点击运行就完事了

下面分别详细的给大家讲解是如何实现的

程序使用教程

1.获取 .exe 程序

2.在windows上点击运行后,会弹出一个微信登陆的二维码,用手机微信扫描,确认登录

3.登陆成功后,程序会显示正在保存的头像,最后会在程序运行的目录生成一张 all.png 的图片

当看到 “所有的微信头像已合成,请查阅all.png!” 的时候,你要的头像墙就在 [wxImages] 文件夹里面

代码教程

代码其实很简单,主要是做起来觉得很有意义,如果你会python基础,再加上下面的讲解,你也可以的!

首先新建一个虚拟环境。为什么要虚拟环境?怎么建虚拟环境? 我之前的文章有写,去历史消息翻翻就能找到

虚拟环境

虚拟环境的名字随意取,我取的是 [“wx”]

在pycharm 中导入刚才建好的虚拟环境

3.需要安装的库:

wxpy 用来操作微信的,除了获取头像,还能给好友发消息,具体可查看官方文档 pillow <=4.2.1 处理头像 pyinstaller

将代码打包成 .exe 程序的

接下来就是写代码了

微信登陆部分代码

1@staticmethod2 defget_image():3 path = os.path.abspath(".") #当前目录

4 bot = Bot() #机器人对象

5 friends = bot.friends(update=True)6 dirs = path + "\\wxImages" #微信头像保存的路径

7 if notos.path.exists(dirs):8 os.mkdir("wxImages")9

10 index =011 for friend infriends:12 print(f"正在保存{friend.nick_name}的微信头像")13 friend.get_avatar(dirs + "\\" + f"{str(index)}.jpg")14 index += 1

15

16 return dirs #合成头像的时候需要用到

合成图像代码

1@staticmethod2 defcomposite_image(dirs):3 images_list =os.listdir(dirs)4 images_list.sort(key=lambda x: int(x[:-4])) #根据头像名称排序

5 length = len(images_list) #头像总数

6 image_size = 2560 #

7 #每个头像大小

8 each_size = math.ceil(image_size /math.floor(math.sqrt(length)))9 lines = math.ceil(math.sqrt(length)) #列数

10 rows = math.ceil(math.sqrt(length)) #行数

11 image = Image.new('RGB', (each_size * lines, each_size *rows))12 row =013 line =014 os.chdir(dirs) #切换工作目录

15 for file in images_list: #遍历每个头像

16 try:17with Image.open(file) as img:18 img =img.resize((each_size, each_size))19 image.paste(img, (line * each_size, row *each_size))20 line += 1

21 if line == lines: #一行填满后开始填下一行

22 line =023 row += 1

24 exceptIOError:25 print(f"头像{file}异常,请查看")26 continue

27

28 img = image.save(os.getcwd() + "/all.png") #将合成的头像保存

29 if notimg:30 print('所有的微信头像已合成,请查阅all.png!')

核心代码完成后,将两部分合一起再导入需要的包,就完事了

源码在此

1#coding: utf-8

2from wxpy importBot, Chat

3importmath

4importos

5from PIL importImage67classWxFriendImage(Chat):8@staticmethod9 defget_image():10 path = os.path.abspath(".")11 bot = Bot() #机器人对象

12 friends = bot.friends(update=True)13

14 dirs = path + "\\wxImages"

15 if notos.path.exists(dirs):16 os.mkdir("wxImages")17

18 index =019 for friend infriends:20 print(f"正在保存{friend.nick_name}的微信头像")21 friend.get_avatar(dirs + "\\" + f"{str(index)}.jpg")22 index += 1

23

24 returndirs25

26@staticmethod27 defcomposite_image(dirs):28 images_list =os.listdir(dirs)29 images_list.sort(key=lambda x: int(x[:-4])) #根据头像名称排序

30 length = len(images_list) #头像总数

31 image_size = 2560

32 #每个头像大小

33 each_size = math.ceil(image_size /math.floor(math.sqrt(length)))34 lines = math.ceil(math.sqrt(length)) #列数

35 rows = math.ceil(math.sqrt(length)) #行数

36 image = Image.new('RGB', (each_size * lines, each_size *rows))37 row =038 line =039os.chdir(dirs)40 for file inimages_list:41 try:42with Image.open(file) as img:43 img =img.resize((each_size, each_size))44 image.paste(img, (line * each_size, row *each_size))45 line += 1

46 if line ==lines:47 line =048 row += 1

49 exceptIOError:50 print(f"头像{file}异常,请查看")51 continue

52 img = image.save(os.getcwd() + "/all.png")53 if notimg:54 print('所有的微信头像已合成,请查阅all.png!')

55defmain():56 dirs =WxFriendImage.get_image()57WxFriendImage.composite_image(dirs)

58if __name__ == '__main__':59 main()

可以将代码复制到自己的编译器里面运行,效果是一样的。

至于打包成 .exe的程序就更简单了

在命令行中运行下面的命令即可

1pyinstaller -F F:\wx\wx.py

运行成功后,会在倒数第二行显示生成程序的保存路径

好了,以上就是两种用python合成微信好友头像的方法

合成之后,可以发到自己的朋友圈,让别人来找找自己的头像在哪,顺便自己还能装个逼,哈哈~`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值