Python实践-itchat获取微信好友头像并进行矩形拼接

1 反正吧,小编也是刚学习,自从发现了itchat之后,就总想捣鼓点什么东西,这不,就想到了这个实例进行实践操作

2 实验代码

#coding:utf-8
#爬取微信好友头像并进行拼接
import itchat
import math
import PIL.Image as Image
import os
#登录
itchat.auto_login(True)
#获取好友信息
friends = itchat.get_friends(update=True)[0:](当然你也可以进行输出,但是小编是看的头晕)
#获取好友头像并保存在目录文件夹下
num = 0
for friend in friends:
    image = itchat.get_head_img(userName=friend["UserName"]) #用 itchat.get_head_img(userName=None)来爬取好友列表的头像
    fileImage = open("/home/guo/桌面/Work/chat/img/" + "/" + str(num) + ".jpg",'wb') #将好友头像下载到本地文件夹
    fileImage.write(image)
    fileImage.close()
    num += 1

#将微信图像进行拼接
dirs = os.listdir("/home/guo/桌面/Work/chat/img/")
each_size = int(math.sqrt(float(640*640)/len(dirs)))
line = int(640.0/each_size)
photographic = Image.new("RGB",(640,640))
x = 0
y = 0
for i in range(0,len(dirs)):
    try:
        imageOfFriends = Image.open("/home/guo/桌面/Work/chat/img/" + "/" + str(i) + ".jpg") #打开一张照片,PIL库的应用
    except IOError:
        print "error"
    else:
        image_resize = imageOfFriends.resize((each_size,each_size))
        photographic.paste(image_resize,(x*each_size,y*each_size))
        x += 1
        if x == line:
            x = 0
            y += 1
#保存图像,发送给文件助手,显示图像
photographic.save("/home/guo/桌面/Work/chat/img/" + "/" + "all.jpg")
itchat.send_image("/home/guo/桌面/Work/chat/img/" + "/" + "all.jpg","filehelper") #把图片发送给微信文件助手(filehelper
photographic.show()

3  至于最后的结果截图,小编就不上传了,但是代码绝对是可行的!!!

 

[项目Github地址](https://github.com/King-Key/Test/blob/itchat/itchat/itchat-1.py)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值