Python gets all WeChat friends' avatar pictures and merges them

#-*- coding:utf-8 -*-

import itchat
import math
import os
import PIL.Image as Image

#Pass the value of true hotReload to the auto_login method. Even if the program is closed, it can be reopened within a certain period of time without rescanning the code 
itchat.auto_login(hotReload= True)
friends = itchat.get_friends(update=True)

#Download avatar pictures of all friends num 
= 0
 for i in friends:
    img = itchat.get_head_img(i["UserName"])
    with open('./headImg/' + str(num) + ".jpg", 'wb') as f:
        f.write(img)
        f.close()
        num += 1
 #Get the number of files in the folder 
length = len(os.listdir( ' ./headImg ' ))
 # Find the size of each one according to the total area 
each_size = int(math.sqrt(float(810 * 810 ) / length))
 #How many lines can be placed in each line 
= int(810 / each_size) #Generate
 a new image with a white background 
image = Image.new( ' RGBA ' , (810, 810), ' white ' )
x = 0
y = 0
for i in range(0, length):
    try:
        img = Image.open('./headImg/' + str(i) + ".jpg")
    except IOError:
        print(i)
        print("Error")
    else:
        img = img.resize((each_size, each_size), Image.ANTIALIAS)  # resize image with high-quality
        image.paste(img, (x * each_size, y * each_size))
        x += 1
        if x == lines:
            x = 0
            y += 1 
image.save( ' ./headImg/ ' + " all.jpg " )
 #Send to your WeChat through the file transfer assistant 
itchat.send_image( ' ./headImg/ ' + " all.jpg " , ' filehelper ' )
image.show()

 

Guess you like

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