Teach you to use Python to make WeChat friend background wall

Contents: 0 Introduction 1 Environment 2 Code Implementation 3 Postscript

0 Preface

Some time ago, a new form of photo sharing began to appear in WeChat Moments, the WeChat friend wall, which displays the avatars of all of your WeChat friends in a large picture.

The effect is as shown in the figure below. For privacy reasons, it is blurred here.

Image: https://uploader.shimo.im/f/F437WsW3JKAxQOOM.png

Isn't it dazzling, and this is unique, after all, everyone's WeChat friends are different. This article will teach you to use Python to achieve this effect.

1 Environment

Operating System: Windows

Python version: 3.7.3

2 code implementation

We need to first obtain the avatar information of the friend, and then process the image and complete the image stitching.

2.0 Preparations

Here, we log in to WeChat to get friend information, using the wxpy module; process and generate the final image with the help of the PIL module. Because they are all third-party modules, if there is no environment in the environment, you can use pip to install them. In addition to the processing of paths, we need to import the os module and the sys module.

from wxpy import *
import PIL.Image as Image
import os
import sys

2.1 Obtain and store friend avatar information

To get the avatars of WeChat friends, we first need to log in to WeChat

# 初始化机器人,扫码登陆微信,适用于Windows系统
bot = Bot()

# # Linux系统,执行登陆请调用下面的这句
# bot = Bot(console_qr=2, cache_path="botoo.pkl"

Before obtaining the friend's avatar information, we have to create a directory locally to store the friend's avatar file.

# 获取当前路径信息
curr_dir = get_dir(sys.argv[0])
# 如果FriendImgs目录不存在就创建一个
if not os.path.exists(curr_dir + "FriendImgs/"):
    os.mkdir(curr_dir + "FriendImgs/")

The next step is to get the friend avatar information and store it in the directory created locally.

my_friends = bot.friends(update=True)
# 获取好友头像信息并存储在FriendImgs目录中
n = 0
for friend in my_friends:
    friend.get_avatar(curr_dir + "FriendImgs/" + str(n) + ".jpg")
    n = n + 1

At this time, you can see the saved pictures of WeChat friends' avatars in the local FriendImgs folder.

insert image description here

2.2 Generate WeChat friend wall

To make a WeChat friend wall, just like the big-character posters in the past, just paste the avatars of our downloaded friends one by one.

First, set the size of the WeChat friend wall and use the Image.new() method.

image = Image.new("RGB", (650, 650))

Next, we need to open the pictures of WeChat friends one by one, using the Image.open() method.

img = Image.open(curr_dir + "FriendImgs/" + file_names)

To reset the WeChat avatar image to a small image with a size of 50*50 pixels, use the img.resize() method.

img = img.resize((50, 50), Image.ANTIALIAS)

Then paste the image into our photo wall using the image.paste() method.

image.paste(img, (x * 50, y * 50))

Finally, save the completed photo wall and use the image.save() method.

img = image.save(curr_dir + "WeChat_Friends.jpg")

Now we put the code in this section together as follows:

# 准备生成微信好友头像墙的尺寸
image = Image.new("RGB", (650, 650))


# 定义初始图片的位置
x = 0
y = 0


# 获取下载的头像文件
curr_dir = get_dir(sys.argv[0])
ls = os.listdir(curr_dir + 'FriendImgs')


# 遍历文件夹的图片
for file_names in ls:
    try:
        # 依次打开图片
        img = Image.open(curr_dir + "FriendImgs/" + file_names)
    except IOError:
        continue
    else:
        # 重新设置图片的大小
        img = img.resize((50, 50), Image.ANTIALIAS)
        # 将图片粘贴到最终的照片墙上
        image.paste(img, (x * 50, y * 50))
        # 设置每一行排13个图像
        x += 1
        if x == 13:
            x = 0
            y += 1
# 保存图片为WeChat_Friends.jpg
img = image.save(curr_dir + "WeChat_Friends.jpg

After the code is executed, the final generated rendering is as follows:

(The picture shown here is blurred)insert image description here

3 Postscript

In this article, the size of the photo wall is set to 650 650, and the size of the friend's avatar is 50 50. In this way, the final photo wall has 13 friends in each row, accommodating 13*13 friends in total.

You can adjust the size parameters according to your actual situation to achieve the best effect. Let's go and create your own unique photo wall~!

Well, all the content of this article ends here. In fact, there are many ways to play later, such as: heart wall, round wall, etc. But the logic is the same, that is, the code will be more troublesome to deal with. If you are interested, you can try it yourself first, or wait for me...

The code has been uploaded to Github: https://github.com/MiracleYoung/You-are-Pythonista/tree/master/PythonExercise/Tool/Wechat_Photo_Wall

Pay attention to the public account " Python Column " to learn more interesting Python~

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324143894&siteId=291194637