用 python 代码写了一个一键生成合成微信好友头像的程序,效果以下:python
不会写代码?不要紧!只要你会使用电脑就 ok! 由于除了用代码方式生成外,还建了一个 .exe 的程序,在电脑点击运行就完事了 下面分别详细的给你们讲解是如何实现的windows
1.公众号后台回复 “wx”便可获取 .exe 程序 微信
2.在windows上点击运行,会弹出一个微信登录的二维码,用手机微信扫描,确认登陆。spa
3.登录成功后,会显示保存的头像,最后会在程序运行的目录生成一张 all.png 的图片命令行
当看到 "全部的微信头像已合成,请查阅all.png!" 的时候,你要的头像墙就在 wxImages 文件夹里面3d
代码其实很简单,主要是作起来以为颇有意义,若是你会python基础,再加上下面的讲解,你也能够的!code
虚拟环境的名字随意取,我取的是 “wx”cdn
3.主要用到下面三个库: wxpy 用来操做微信的,除了获取头像,还能给好友发消息,具体可查看官方文档 pillow <=4.2.1 处理头像 pyinstaller 将代码打包成 .exe 程序的 4. 接下来就是写代码了对象
微信登录部分代码blog
@staticmethod
def get_image():
path = os.path.abspath(".") #当前目录
bot = Bot() # 机器人对象
friends = bot.friends(update=True)
dirs = path + "\\wxImages" # 微信头像保存的路径
if not os.path.exists(dirs):
os.mkdir("wxImages")
index = 0
for friend in friends:
print(f"正在保存{friend.nick_name}的微信头像")
friend.get_avatar(dirs + "\\" + f"{str(index)}.jpg")
index += 1
return dirs # 合成头像的时候须要用到
复制代码
合成图像代码
@staticmethod
def composite_image(dirs):
images_list = os.listdir(dirs)
images_list.sort(key=lambda x: int(x[:-4])) # 根据头像名称排序
length = len(images_list) # 头像总数
image_size = 2560 #
# 每一个头像大小
each_size = math.ceil(image_size / math.floor(math.sqrt(length)))
lines = math.ceil(math.sqrt(length)) # 列数
rows = math.ceil(math.sqrt(length)) # 行数
image = Image.new('RGB', (each_size * lines, each_size * rows))
row = 0
line = 0
os.chdir(dirs) # 切换工做目录
for file in images_list: # 遍历每一个头像
try:
with Image.open(file) as img:
img = img.resize((each_size, each_size))
image.paste(img, (line * each_size, row * each_size))
line += 1
if line == lines: # 一行填满后开始填下一行
line = 0
row += 1
except IOError:
print(f"头像{file}异常,请查看")
continue
img = image.save(os.getcwd() + "/all.png") # 将合成的头像保存
if not img:
print('全部的微信头像已合成,请查阅all.png!')
复制代码
核心代码完成后,将两部分合一块儿再导入须要的包,就完事了 源码在此
# coding: utf-8
from wxpy import Bot, Chat
import math
import os
from PIL import Image
class WxFriendImage(Chat):
@staticmethod
def get_image():
path = os.path.abspath(".")
bot = Bot() # 机器人对象
friends = bot.friends(update=True)
dirs = path + "\\wxImages"
if not os.path.exists(dirs):
os.mkdir("wxImages")
index = 0
for friend in friends:
print(f"正在保存{friend.nick_name}的微信头像")
friend.get_avatar(dirs + "\\" + f"{str(index)}.jpg")
index += 1
return dirs
@staticmethod
def composite_image(dirs):
images_list = os.listdir(dirs)
images_list.sort(key=lambda x: int(x[:-4])) # 根据头像名称排序
length = len(images_list) # 头像总数
image_size = 2560
# 每一个头像大小
each_size = math.ceil(image_size / math.floor(math.sqrt(length)))
lines = math.ceil(math.sqrt(length)) # 列数
rows = math.ceil(math.sqrt(length)) # 行数
image = Image.new('RGB', (each_size * lines, each_size * rows))
row = 0
line = 0
os.chdir(dirs)
for file in images_list:
try:
with Image.open(file) as img:
img = img.resize((each_size, each_size))
image.paste(img, (line * each_size, row * each_size))
line += 1
if line == lines:
line = 0
row += 1
except IOError:
print(f"头像{file}异常,请查看")
continue
img = image.save(os.getcwd() + "/all.png")
if not img:
print('全部的微信头像已合成,请查阅all.png!')
def main():
dirs = WxFriendImage.get_image()
WxFriendImage.composite_image(dirs)
if __name__ == '__main__':
main()
复制代码
能够将代码复制到本身的编译器里面运行,效果是同样的。 至于打包成 .exe的程序就更简单了 在命令行中运行下面的命令便可
pyinstaller -F F:\wx\wx.py
复制代码
运行成功后,会在倒数第二行显示生成程序的路径 好了,以上就是用python合成微信好友头像的所有指南了 以为对你有用,就帮忙点个赞呗...