Python—itchat下载拼接微信好友头像图

#!/usr/bin/env python3
# -*- coding:utf-8 -*-

#获取微信好友图片

import itchat

itchat.auto_login()

for friend in itchat.get_friends(update=True)[0:]:
    print(friend['NickName'], friend['RemarkName'], friend['Sex'], friend['Province'], friend['Signature'])
    img = itchat.get_head_img(userName=friend['UserName'])
    path = "/itchat/" + friend["NickName"] +"(" + friend["RemarkName"] + ").jpg"
    try:
        with open(path,'wb') as f:
            f.write(img)
    except Exception as e:
        print(repr(e))
itchat.run()


好用图片拼接成一幅图片

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import os
from math import sqrt
from PIL import Image

path = "/home/alex/清纯妹子/"

pathList = []

for item in os.listdir(path):
    imgPath = os.path.join(path,item)
    pathList.append(imgPath)

total = len(pathList) #total
line = int(sqrt(total)) #行数
NewImage = Image.new('RGB',(128*line,128*line))
x=y=0
for item in pathList:
    try:
        img = Image.open(item)
        img = img.resize((128,128),Image.ANTIALIAS)
        NewImage.paste(img,(x*128,y*128))
        x+=1
    except IOError:
        print("第%d行,%d列文件读取失败!IOERROR:%s" %(y,x,item))
        x-=1
    if x == line:
        x=0
        y+=1
    if(x+line*y) == line *line:
        break
NewImage.save(path+"final.jpg")

猜你喜欢

转载自my.oschina.net/yonghan/blog/1648502