最近玩了玩一些和python有关的小代码,感觉python在处理这些实用性的问题上还是比C和Java强一些,可能这就是静态语言和脚本语言的区别吧
这段代码中唯一需要修改的就是路径
下载部分 getHeadImgs.py
import itchat
itchat.auto_login()
for friend in itchat.get_friends(update=True)[0:]:
print(friend['NickName'],friend['RemarkName'],friend['Sex'],friend['Province'],friend['Signature'])
img = itchat.get_head_img(userName=friend["UserName"])
path = "C:\\Users\\Administrator\\Desktop\\map1\\"+friend['NickName']+"("+friend['RemarkName']+").jpg"
try:
with open(path,'wb') as f:
f.write(img)
except Exception as e:
print(repr(e))
itchat.run()
拼接部分jointHeadImgs.py
import os
from math import sqrt
from PIL import Image
path = 'C:\\Users\\Administrator\\Desktop\\map1\\'
pathList = []
for item in os.listdir(path):
imgPath = os.path.join(path,item)
pathList.append(imgPath)
total = len(pathList)
line = int(sqrt(total))
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(path+"final.jpg")