今天在朋友圈看到有人发了微信好友拼接图,心里满是新奇,看了下评论才知道用Python写的。心里痒痒,立马就安装了下Python。
安装好了之后,看了下大神的代码,基本上能够读得懂(语言都是想通的嘛!),然后就尝试在小黑窗运行了,结果报错了!
rawmode = RAWMODE[im.mode] KeyError: ‘RGBA‘
这种错误看的我是一脸懵逼啊,搜索了半天也没看到什么解决方案,结果就在宁外一篇博客的评论里面发现了解决方法,结果成功运行,还是66的。
import itchat import math import PIL.Image as Image import os itchat.auto_login() friends = itchat.get_friends(update=True)[0:] user = friends[0]["UserName"] num = 0 for i in friends: img = itchat.get_head_img(userName=i["UserName"]) fileImage = open(‘D:‘ + "/wechat_head_image/" + str(num) + ".jpg",‘wb‘) fileImage.write(img) fileImage.close() num += 1 ls = os.listdir(‘D:/wechat_head_image‘) each_size = int(math.sqrt(float(640*640)/len(ls))) lines = int(640/each_size) image = Image.new(‘RGB‘, (640, 640)) x = 0 y = 0 for i in range(0,len(ls)+1): try: img = Image.open(‘D:/wechat_head_image‘ + "/" + str(i) + ".jpg") except IOError: print("Error") else: img = img.resize((each_size, each_size), Image.ANTIALIAS) image.paste(img, (x * each_size, y * each_size)) x += 1 if x == lines: x = 0 y += 1 image.save(‘D:/wechat_head_image/all‘ + "/" + "all.jpg") itchat.send_image(‘D:/wechat_head_image/all‘ + "/" + "all.jpg", ‘filehelper‘)
运行后效果截图
时间: 2024-10-09 08:29:46