代码
import itchat,math
import os
import re
import random
from PIL import Image
from wordcloud import WordCloud
import matplotlib.pyplot as plt
def get_friend():
//登陆微信
itchat.auto_login(True)
//得到微信好友信息,返回字典类型的信息
friends = itchat.get_friends(update=True)
//图片保存路径
pic_path = os.getcwd() + '/pic/'
//如果pic文件夹路径不存在,就创建一个
if not os.path.exists(pic_path):
os.mkdir(pic_path)
//遍历所有好友信息
for i,friend in enumerate(friends):
//得到微信头像
img_byte = itchat.get_head_img(userName=friend['UserName'])
//打开一张空图片
img = open(pic_path+str(friend['UserName'])+'.jpg','wb')
//将微信头像写入空图片
img.write(img_byte)
//记得关闭打开的空图片
img.close()
def get_images():
x = 0
y = 0
pic_path = os.getcwd()
//得到pic文件夹下面所有文件名列表
imgs = os.listdir('pic')
//打乱文件顺序
random.shuffle(imgs)
//创建一张1024*1024大小的图片
newImage = Image.new('RGBA',(1024,1024))
//得到所有将要填充图片的计算宽度
width = int(math.sqrt(1024*1024/len(imgs)))
//一行需要填充图片的个数
numline = int(1024/width)
for i in imgs:
if not i.endswith('jpg'):
continue
img = Image.open('pic/'+i)
img = img.resize((width,width),Image.ANTIALIAS)
newImage.paste(img,(x*width,y*width))
x += 1
if x>=numline:
x=0
y+=1
newImage.save(pic_path+"all.png")
效果
哈哈,猜一猜那个是我?