Teach you to use Python backdrop made micro-letter friends

Contents:
Introduction
1 Environmental
2 code to achieve
3 postscript

0 Preface

Some time ago, micro-channel circle of friends began the emergence of a new form of sun photo, micro-letter friends wall, that demonstrate all of their micro-channel friend's head on a large picture.

The following picture, for privacy reasons, made a blur here.

It is not flashy, but this is unique, after all, everyone's micro-letter friends is not the same. This article will teach you use Python to achieve this effect.

1 environment

Operating System: Windows

Python Version: 3.7.3

2 code implementation

We need first of all need to get the friend's head of information, the next image processing and image stitching is completed.

2.0 Preparations

Here, we get to log micro letter friends information, using wxpy module; and generate the final image processing means PIL module. Because all third-party modules, such as the environment can not be installed using pip. Further processing involving path, and we need to import the os module sys module.

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

2.1 acquires and stores information friends head

We want to get micro-channel friend's head, you first need to log micro letter

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

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

Before acquiring friends head of information, we have to create a directory on the local, friends head for subsequent storage of files.

# 获取当前路径信息
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 obtain information Friends Avatar and stores it in a local directory created.

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

Then you can locally FriendImgs folder, see preserved micro-letter friends head of the picture.

在这里插入图片描述

2.2 generating a micro-channel wall Friends

Production of micro-letter friends wall, just like the previous poster, we downloaded the picture one by one friend posted up to.

First, a good sized micro-letter friends wall, use Image.new () method.

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

Next, we need to open one by one micro-letter friends pictures, use Image.open () method.

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

The micro-channel avatar image is reset to the size of 50 * 50 pixel thumbnail using img.resize () method.

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

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

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

Finally, save the finished photo wall down, use image.save () method.

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

We will now present the code section integrated 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, to generate the final effect is as follows:

(Pictures shown here do blur)

Postscript 3

本文中设定照片墙尺寸为650650,而好友头像尺寸为5050,这样最终制作成的照片墙每行有13位好友,共计容纳13*13位好友。

大家可根据自己实际情况,自行调整尺寸参数,以达到最佳效果。大家赶快去生成自己独一无二的照片墙吧~!

好啦,本文所有内容就到这里结束啦。其实后面还可以衍生很多玩法,比如:心行墙、圆墙等。不过逻辑一样,就是代码处理起来会比较麻烦。有兴趣的可以先自己试试,或者等我……

代码已上传至Github:https://github.com/MiracleYoung/You-are-Pythonista/tree/master/PythonExercise/Tool/Wechat_Photo_Wall

关注公众号「Python专栏」了解更多有趣的Python~

Guess you like

Origin www.cnblogs.com/moonhmily/p/11200162.html