Python实现好友全头像的拼接实例(推荐)

yipeiwu_com6年前Python基础

微信好友全头像

话不多说,直接上代码

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

itchat.auto_login()
friends = itchat.get_friends(update=True)[0:]
user = friends[0]["UserName"]

num = 0
for i in friends:
 img = itchat.get_head_img(userName=i["UserName"])
 fileImage = open('文件夹' + "/" + str(num) + ".jpg",'wb')
 fileImage.write(img)
 fileImage.close()
 num += 1

ls = os.listdir('文件夹')
each_size = int(math.sqrt(float(640*640)/len(ls)))
lines = int(640/each_size)
image = Image.new('RGBA', (640, 640))
x = 0
y = 0
for i in range(0,len(ls)+1):
 try:
  img = Image.open('文件夹' + "/" + str(i) + ".jpg")
 except IOError:
  print("Error")
 else:
  img = img.resize((each_size, each_size), Image.ANTIALIAS)
  image.paste(img, (x * each_size, y * each_size))
  x += 1
  if x == lines:
   x = 0
   y += 1
image.save('文件夹' + "/" + "all.jpg")
itchat.send_image('文件夹' + "/" + "all.jpg", 'filehelper')

代码运行需要安装两个库

pip install itchat
pip install pillow

如果安装python的时候pip安装选项没打√ ,就先安装pip。

Python和pip的安装

itchat官方介绍

代码运行过程中会出现登录二维码,用微信扫一下,你就可以看到处理的进度。一会你的微信文件传输助手就会收到拼接好的头像图片。

以上这篇Python实现好友全头像的拼接实例(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Pandas 解决dataframe的一列进行向下顺移问题

最近做比赛,有时候需要造出新的特征,而这次遇到的问题是将一列数据往下顺移一位。同时将开头缺失的那一个数据用其他方式填充。 df['feature'].shift(1)向下顺移一位,这时第...

Python2和Python3中urllib库中urlencode的使用注意事项

前言 在Python中,我们通常使用urllib中的urlencode方法将字典编码,用于提交数据给url等操作,但是在Python2和Python3中urllib模块中所提供的urle...

Django自带日志 settings.py文件配置方法

Django settings.py文件配置部分: # logging配置 log_file = '/home/nagain/learn/log' log_file_path = o...

python正则表达式匹配[]中间为任意字符的实例

如下所示: result = re.search('^\[[\S\s]*\]$',str) print(result) print(result.group()) <_sre....

CentOS 7下安装Python3.6 及遇到的问题小结

先给大家介绍下CentOS 7下安装Python3.6 的方法 安装python3.6可能使用的依赖 yum install openssl-devel bzip2-devel ex...