Py学习  »  Python

用Python一键生成微信好友头像墙

Pyhon编程与实战 • 4 年前 • 487 次点击  
阅读 6

用Python一键生成微信好友头像墙

前言

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

不会写代码?没关系!只要你会使用电脑就 ok! 因为除了用代码方式生成外,还建了一个 .exe 的程序,在电脑点击运行就完事了 下面分别详细的给大家讲解是如何实现的

程序使用教程

1.公众号后台回复 “wx”即可获取 .exe 程序

20190421163538.png

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

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

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

20190421164723.png

  1. 你可以把这张图发到朋友圈,随便配个文案,随后就等着大波点赞吧。

代码教程

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

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

虚拟环境

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

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

3.主要用到下面三个库: wxpy 用来操作微信的,除了获取头像,还能给好友发消息,具体可查看官方文档 pillow <=4.2.1 处理头像 pyinstaller 将代码打包成 .exe 程序的 4. 接下来就是写代码了

微信登陆部分代码

@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合成微信好友头像的全部指南了 觉得对你有用,就帮忙点个赞呗...

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/34267
 
487 次点击