欢迎光临
我们一直在努力

利用python抓取所有微信好友头像并拼接成照片墙

# Author:Qist Fu
import itchat
import os

import PIL.Image as Image
from os import listdir
import math

itchat.auto_login()

friends = itchat.get_friends(update=True)[0:]

user = friends[0]["UserName"]

print(user)

os.mkdir(user)

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

pics = listdir(user)

numPic = len(pics)

print(numPic)

eachsize = int(math.sqrt(float(640 * 640) / numPic))

print(eachsize)

numline = int(640 / eachsize)

toImage = Image.new('RGB', (640, 640)) #爬取完成,调用PIL库时候会提示无法生成JPEG,报错raise IOError("cannot write mode %s as JPEG" % im.mode)IOError: cannot write mode RGBA as JPEG后来将'RGBA'改成了'RGB'就正常了


raise IOError("cannot write mode %s as JPEG" % im.mode)
IOError: cannot write mode RGBA as JPEG




print(numline)

x = 0
y = 0

for i in pics:
	try:
		#打开图片
		img = Image.open(user + "/" + i)
	except IOError:
		print("Error: 没有找到文件或读取文件失败")
	else:
		#缩小图片
		img = img.resize((eachsize, eachsize), Image.ANTIALIAS)
		#拼接图片
		toImage.paste(img, (x * eachsize, y * eachsize))
		x += 1
		if x == numline:
			x = 0
			y += 1


toImage.save(user + ".jpg")

#
itchat.send_image(user + ".jpg", 'filehelper')

 

未经允许不得转载:第五博客 » 利用python抓取所有微信好友头像并拼接成照片墙
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

更专业 更方便

联系我们