Python之itchat生成微信头像拼接问题
2017-11-26 14:16
691 查看
#coding=utf8 import itchat import os import PIL.Image as Image from os import listdir import math itchat.auto_login(enableCmdQR=False) friends = itchat.get_friends(update=True)[0:] user = friends[0]["UserName"] print(user) os.mkdir(user) num = 0 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('RGBA', (640, 640)) 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 wxImage.py爬取完成,调用PIL库时候会提示无法生成JPEG
raise IOError("cannot write mode %s as JPEG" % im.mode)
IOError: cannot write mode RGBA as JPEG
#原因
查询资料,发现其实是因为JPG图像有RGBA四个通道
所以调用含有A通道(透明度)方法/参数时候,程序不知道A通道怎么办,就会产生错误。
#解决办法
调用 Image.new 该方法时候,传进‘RGB’,而不是‘RGBA’Image.new('RGB', (640, 640))
相关文章推荐
- 使用Python itchat个人微信账号头像拼接
- 用python itchat包 爬取微信好友头像形成矩形头像集
- itchat+pillow实现微信好友头像爬取和拼接
- Python利用itchat对微信中好友数据实现简单分析的方法
- python实现微信接口(itchat)
- 使用Python微信库itchat获得好友和群组已撤回的消息
- 有了 itchat, python 调用微信个人号从未如此简单(新增 py3 支持)
- Python学习-Itchat微信API
- Python微信库:itchat的用法详解
- python实现微信接口(itchat)
- python微信模块 itchat
- python实现微信接口(itchat)
- 使用python抓取微信好友个性签名并形成词云(itchat;wordcloud)
- python itchat实现微信好友头像拼接图的示例代码
- Python微信好友信息意义及取值分析(基于itchat)
- python实现微信接口(itchat)
- [置顶] 【python 图像拼接】 微信好友头像下载和拼接技术
- python微信库:itchat
- python-itchat微信消息群发、自动回复
- Python实现微信好友签名词云的构建(itchat、jieba、wordcloud)