python代码练习,微信登入并生成头像大图
生活随笔
收集整理的這篇文章主要介紹了
python代码练习,微信登入并生成头像大图
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
import itchat
import os
from math import sqrt
from PIL import Image
import shutilpwd = 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是好友頭像圖片總數(shù)
line = int(sqrt(total)) #line是拼接圖片的行數(shù)(即每一行包含的圖片數(shù)量)
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 += 1except IOError:print("第%d行,%d列文件讀取失敗!IOError:%s" % (y,x,item))x -= 1if x == line:x = 0y += 1if (x+line*y) == line*line:break
NewImage.save(filename+'\\'+user+'.jpg')
itchat.send_image(filename+'\\'+user+'.jpg', 'filehelper') #發(fā)送圖片到文件助手
itchat.send('圖片生成成功', 'filehelper')
shutil.rmtree(filename) #刪除文件夾
itchat.logout()
網(wǎng)上有很多類似的代碼,吵吵改改,自己寫的勿噴,代碼練習(xí)中
有個(gè)問題itchat的登入二維碼會(huì)在程序關(guān)閉后還會(huì)彈出來有什么解決辦法嗎
總結(jié)
以上是生活随笔為你收集整理的python代码练习,微信登入并生成头像大图的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 线性规划Matlab程序(小白向)
- 下一篇: python怎么撤销_关于Python: