趣味学python(008)

python应用——微信照片墙

最近小编学习了微信照片墙的制作,特来分享一下!????
运用到的模块itchat,pillow两个包。
itchat用于获取好友头像等信息,pillow时python中的图像处理,用他来拼接好友图像。
运用pip install itchat,下载即可,pillow同理

import itchat
import os
import math
from PIL import Image

微信照片墙的构建

def create_imag():
# 把头像变成照片墙
imgs = os.listdir(‘img’)
# 准备一面墙,用来装图片
newImg = Image.new(‘RGBA’, (860, 860))
# 要将照片缩小,在粘贴上去
# 得到照片的实际大小,按次序放上去
width = int(math.sqrt((860860)/len(imgs)))
numLine = int(860/width)
x = 0
y = 0
for i in imgs:
# 先打开一个文件
img = Image.open(‘img/’ + i)
# 对照片进行等比例的缩放
img = img.resize((width, width), Image.ANTIALIAS)
newImg.paste(img, (x
width, y*width))
x += 1
# 判断当前一行是否排满了,就让x回归为0,y加一
if x >= numLine:
x = 0
y += 1
# 生成的照片墙保存一下
newImg.save(‘all.png’)
print(‘照片墙已经生成好了’)

构建主函数

def main():
# 登录微信(网页版)
itchat.auto_login(hotReload=True)
# 获取好友的信息 实时更新
friends = itchat.get_friends(update=True)
# 判断img目录是否存在
if not os.path.exists(‘img’):
os.mkdir(‘img’)
# 同时得到索引和值
for count, friend in enumerate(friends):
# 获取好友的头像
img = itchat.get_head_img(userName=friend[“UserName”])
# 将二进制数据转化为图片数据
if count <= 20:
with open(‘img/’ + str(count) + ‘.jpg’, ‘wb’) as f:
f.write(img)
f.close()
else:
print(“完成了”)
break

if name == ‘main’:
main()
create_imag()

但是小编的微信网页版登不上去???????,这就有点难受了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值