Python key to making a micro-channel buddy picture wall method

How to achieve with Python code, the code is not long, 60 lines can handle.

The core is the use of three libraries:

  • wxpy library used to get friends head and then download
  • Pillow library for stitching Avatar
  • Pyinstaller library, used to package Python program into exe file

Program implemented by three functions, the first function generating creat_filepath file path of the image, the second micro-channel acquisition cycles save_avatar function and saved to a local friends head, the third head joint_avatar function is to assemble into a larger image.

The complete code is as follows:

from wxpy import *
import math
from PIL import Image
import os
'''
遇到python不懂的问题,可以加Python学习交流群:1004391443一起学习交流,群文件还有零基础入门的学习资料
'''
# 创建头像存放文件夹
def creat_filepath():
 avatar_dir = os.getcwd() + "\\wechat\\"
 if not os.path.exists(avatar_dir):
 os.mkdir(avatar_dir)
 return avatar_dir
# 保存好友头像
def save_avatar(avatar_dir):
 # 初始化机器人,扫码登陆
 bot = Bot()
 friends = bot.friends(update=True)
 num = 0
 for friend in friends:
 friend.get_avatar(avatar_dir + '\\' + str(num) + ".jpg")
 print('好友昵称:%s' % friend.nick_name)
 num = num + 1
# 拼接头像
def joint_avatar(path):
 # 获取文件夹内头像个数
 length = len(os.listdir(path))
 # 设置画布大小
 image_size = 2560
 # 设置每个头像大小
 each_size = math.ceil(2560 / math.floor(math.sqrt(length)))
 # 计算所需各行列的头像数量
 x_lines = math.ceil(math.sqrt(length))
 y_lines = math.ceil(math.sqrt(length))
 image = Image.new('RGB', (each_size * x_lines, each_size * y_lines))
 x = 0
 y = 0
 for (root, dirs, files) in os.walk(path):
 for pic_name in files:
 # 增加头像读取不出来的异常处理
 try:
 with Image.open(path + pic_name) as img:
 img = img.resize((each_size, each_size))
 image.paste(img, (x * each_size, y * each_size))
 x += 1
 if x == x_lines:
 x = 0
 y += 1
 except IOError:
 print("头像读取失败")
 img = image.save(os.getcwd() + "/wechat.png")
 print('微信好友头像拼接完成!')
if __name__ == '__main__':
 avatar_dir = creat_filepath()
 save_avatar(avatar_dir)
 joint_avatar(avatar_dir)

You can run the program file, it can also run directly after Pyinstaller file package. Here to talk about additional methods and Closure Guidelines pyinstaller packaged.

Do not directly pyinstaller packaged with the system, or a large package out of exe files. It recommends packaged in a virtual environment, packed out of exe files are much smaller, about 10MB.

Create a virtual environment is very simple, simply click the steps of:

1 installation pipenv and pyinstaller package, to create a virtual environment and for subsequent Packager:

pip install pipenv
pip install pyinstaller # 已安装就不用安装了

2 Select an appropriate directory as a Python virtual environment, run:

pipenv install # 创建虚拟环境
pipenv shell # 创建好后,进入虚拟环境

3 installer referenced libraries, the above procedure references four libraries: wxpy, math, os and PIL, a line of code to complete the installation.

pipenv install wxpy math os

4 extra attention here to install PIL, now do not PIL library, but replaced with Pillow library, so the installation Pillow library on the line. But do not install the latest version 6.0.0, otherwise you may encounter a variety of errors, such as: PIL is not recognized downloaded jpg picture files.

OSError: cannot identify image file <ImageFieldFile: images

The correct installation method is to install a lower version, by trying to install version 4.2.1 is no problem, install the command:

pipenv install Pillow==4.2.1

5 then packaged program on it:

pyinstaller -F C:\Users\sony\Desktop\wechat_avatar.py 
# 程序路径要改成你电脑上的路径
# -F 表示生成单个 exe 文件,方便运行

Run as follows:

Run command, about 1 minute if successfully word indicates that the program displays Packaging Success:

Then in the program directory to find the  wechat_avatar.exe file, and then follow the first method as running on the line. 

Guess you like

Origin blog.csdn.net/qq_40925239/article/details/92641472