import os
import math
import wxpy # 通过程序操作微信
from PIL import Image
from PIL import ImageFile
class AvatarWall(object):
def __init__(self, dir_name="avatars", img_name="avatar_wall.jpg", img_size=2400):
# 当前路径作为根路径
self.path = os.getcwd()
self.avatar_dir = os.path.join(self.path, dir_name)
# 通过机器人对象获取微信号好友列表
self.bot = wxpy.Bot()
self.friends = self.bot.friends(update=True)
self.image_name = img_name
self.image_size = img_size
# def get_friends(self):
# """获取当前微信号的好友列表"""
# return self.wechat.friends(update=True)
def save_avatars(self):
self.bot.enable_puid(os.path.join(self.path, "wxpy_puid.pkl"))
self.bot.enable_puid()
if not os.path.exists(self.avatar_dir):
os.mkdir(self.avatar_dir)
for friend in self.friends:
try:
name = friend.name
friend.get_avatar(save_path=os.path.join(self.avatar_dir, name+".jpg"))
except FileNotFoundError:
name = friend.puid
friend.get_avatar(save_path=os.path.join(self.avatar_dir, name + ".jpg"))
print("已经存储好友{}的头像".format(name))
print("您共有{}位微信好友, 头像已经全部存储到{}路径下".format(len(self.friends), self.avatar_dir))
def generate_avatar_wall(self):
ImageFile.LOAD_TRUNCATED_IMAGES = True
# x, y方向上照片的个数, 要是多出一行可以手动减一
x_count = y_count = math.ceil(math.sqrt(len(self.friends)))
item_size = math.floor(self.image_size/math.floor(math.sqrt(len(self.friends))))
# 只接受int 类型的size
avatar_image = Image.new("RGB", (x_count*item_size, y_count*item_size))
avatars = os.listdir(self.avatar_dir)
x = y = 0
for avatar in avatars:
avatar_path = os.path.join(self.avatar_dir, avatar)
try:
item = Image.open(avatar_path)
except IOError:
continue
else:
item = item.resize((item_size, item_size))
avatar_image.paste(item, (x*item_size, y*item_size))
x += 1
# 进入下一行
if x == x_count:
x = 0
y += 1
avatar_image.save(os.path.join(self.path, self.image_name))
print("好友头像墙已生成, 存储在{}目录下,快去看看吧".format(self.path))
if __name__ == '__main__':
aw = AvatarWall()
aw.save_avatars()
aw.generate_avatar_wall()
# 退出微信网页端
aw.bot.logout()
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
本文主要介绍了如何使用python编写一个微信好友头像墙的源码,这里将会用到一些python的基础知识,比如tkinter模块,matplotlib模块,以及python的图片处理库Pillow。 首先,需要从微信客户端获取好友头像信息,这可以通过编写一段python脚本来实现,脚本首先需要调用微信API接口,获取微信中好友的头像,然后将头像存储到一个文件夹中。 其次,需要使用tkinter模块创建一个GUI界面,用户可以通过GUI界面指定要显示的头像文件夹,以及头像的大小设置。 然后,需要使用matplotlib模块将头像以网格形式显示出来, matplotlib模块提供了许多精美的图形控件,可以让头像墙更加美观。 最后,需要使用Pillow模块处理头像,比如将头像转换为固定大小,以及添加文字等操作,最终完成一个精美的头像墙。 总之,这是一个很有趣的python编程项目,可以让大家了解python的基本功能,同时更加深入的学习python的编程技术,比如tkinter模块,matplotlib模块,以及Pillow模块等。
资源推荐
资源详情
资源评论
收起资源包目录
微信好友头像墙.zip (5个子文件)
微信好友头像墙
QR.png 721B
站长VX-带你搞钱.jpg 103KB
点击查看更多实操项目-艺铭网创.url 113B
avatar_wall.py 3KB
网站-同名公众号.jpg 7KB
共 5 条
- 1
资源评论
w风雨无阻w
- 粉丝: 1700
- 资源: 51
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- WS2-32.lib,在编译程序中可以链接使用
- 秒懂傅里叶变换matlab程序实现过程
- ZEND解密dezender12
- sony 索尼IMX334摄像头模组电路板AD版硬件PCB图(6层板).zip
- 基于flask和echarts融合交易策略的bitfinex可视化微服务.zip
- 包含了wvp-assist.tar wvp-talk.tar zlmediakit.tar .
- 3r4efgh53wgrf43tw
- 2024新版Java基础从入门到精通全套视频+资料下载
- Spring AI大模型视频教程+ChatGPT视频教程+OpenAI大模型视频教程(资料+视频教程)
- ABB工业机器人教程PDF版本
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功