import itchat
import os
from math import sqrt
from PIL import Image
import shutil
pwd = os.getcwd() #获取相对路径
print(pwd)
filename = pwd +'\\'+'weixin' #存放图片路径
print(filename)
if not os.path.exists(filename): #判断是否有文件夹
os.makedirs(filename)
print('文件夹已新建')
else:
print('文件夹已存在')
itchat.auto_login() #微信登入
friends = itchat.get_friends(update=True)[0:]
itchat.send('欢迎光临,正在为您生成图片', 'filehelper')
for item in friends: #开始下载图片
print ('正在下载 '+item['NickName']+' 的图片')
img = itchat.get_head_img(userName=item["UserName"])
path = filename+'\\'+item['NickName']+'.jpg'
try:
with open(path,'wb') as f:
f.write(img)
except Exception as e:
print(repr(e))
user = friends[0]['UserName']
pathList = []
for item in os.listdir(filename):
imgPath = os.path.join(filename,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)
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(filename+'\\'+user+'.jpg')
itchat.send_image(filename+'\\'+user+'.jpg', 'filehelper') #发送图片到文件助手
itchat.send('图片生成成功', 'filehelper')
shutil.rmtree(filename) #删除文件夹
itchat.logout()
网上有很多类似的代码,吵吵改改,自己写的勿喷,代码练习中
有个问题itchat的登入二维码会在程序关闭后还会弹出来有什么解决办法吗