教你生成超高逼格微信朋友圈

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

import itchat
import os
from math import sqrt
from PIL import Image

#imgPath是存放好友头像图的文件夹的路径
headImgPath = './img/'

isExists = os.path.exists(headImgPath)
if not isExists:
	os.makedirs(headImgPath)

itchat.auto_login()
for friend in itchat.get_friends(update=True)[0:]:
    #可以用此句print查看好友的微信名、备注名
    print(friend['NickName']+"("+friend['RemarkName']+")")
    img = itchat.get_head_img(userName=friend["UserName"])
    path = headImgPath+friend['NickName']+"("+friend['RemarkName']+").jpg"
    try:
        with open(path,'wb') as f:
            f.write(img)
    except Exception as e:
        print(repr(e))

pathList = []
# os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表。这个列表以字母顺序。 它不包括 '.' 和'..' 即使它在文件夹中。
for item in os.listdir(headImgPath):
    imgPath = os.path.join(headImgPath, item)
    pathList.append(imgPath)

total = len(pathList)#total是好友头像图片总数
line = int(sqrt(total))#line是拼接图片的行数(即每一行包含的图片数量)
NewImage = Image.new('RGB', (128*line,128*line))
x = y = 0
for item in pathList:
    try:
        img = Image.open(item)
        # Image.ANTIALIAS:平滑滤波。对所有可以影响输出像素的输入像素进行高质量的重采样滤波,以计算输出像素值。
        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("final.jpg")

操作方法参考:https://www.cnblogs.com/dengfaheng/p/10957359.html
代码都在这里:https://github.com/dengfaheng/WeChatCode

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值