├── README.md ├── requirements.txt ├── screenshots ├── 360321781025374987.jpg ├── @7464eb52a847b7cb7698f2f004586e9d22ed5d148a07da30386c2a726e900320.jpg ├── @7c31f485fe852dad33336c52e10565aeab009477fdf0adf28838ca1d35e666da.jpg ├── @f5c98c1d7b53eef38e4db58663ab5f3b93d49f333e81a61afc6c21b349a874f0.jpg └── wxid_eujni1y71a522_1489555725169_73.png ├── wxImage.ipynb └── wxImage.py /README.md: -------------------------------------------------------------------------------- 1 | # itchat+pillow实现微信好友头像爬取和拼接 2 | 3 | ------ 4 | 5 | 本项目[github地址][1] 6 | 7 | ------ 8 | 9 | 10 | ###效果图 11 | 12 | 13 | 14 | 15 | 16 | 17 | ![demo2][3] 18 | 19 | 20 | ![demo3][4] 21 | 22 | 23 | ![demo4][5] 24 | 25 | 26 | ------ 27 | 28 | ### 使用方法(前提是设备安装了python): 29 | 30 | 下载[本项目][6]到本地,打开项目主目录,打开命令行,输入: 31 | 32 | pip install -r requirements.txt 33 | 34 | 等待安装完成,输入: 35 | 36 | python wxImage.py 37 | 38 | 出现如下二维码: 39 | 40 | ![二维码][7] 41 | 42 | 用手机微信右上角的扫一扫,确认登陆即可。 43 | 44 | 稍等片刻,你打开手机微信,找到信息栏的微信传输助手,会看到如下: 45 | 46 | ![微信文件传输助手][9] 47 | 48 | ------ 49 | 50 | ## 核心 51 | 52 | python: 53 | 54 | - itchat(用于爬取头像) 55 | - pillow(用于拼接图片) 56 | 57 | ------ 58 | 59 | ##源码详解 60 | 61 | 首先登陆python版本微信itchat,生成二维码: 62 | 63 | itchat.auto_login(enableCmdQR=True) 64 | 65 | 获取好友列表: 66 | 67 | friends = itchat.get_friends(update=True)[0:] 68 | 69 | 然后使用itchat的get_head_img(userName=none)函数来爬取好友列表的头像,并下载到本地: 70 | 71 | ``` 72 | num = 0 73 | 74 | for i in friends: 75 | img = itchat.get_head_img(userName=i["UserName"]) 76 | fileImage = open(user + "/" + str(num) + ".jpg",'wb') 77 | fileImage.write(img) 78 | fileImage.close() 79 | num += 1 80 | ``` 81 | 计算出每张头像缩小后的尺寸(由于为了拼接之后可以用来作为为微信头像,所以合成的图片大小都是640 * 640的,因为微信头像大小就是640 * 640) 82 | 83 | 计算每张头像缩小后的边长(默认为正方形): 84 | 85 | eachsize = int(math.sqrt(float(640 * 640) / numPic)) 86 | 87 | 计算合成图片每一边分为多少小边: 88 | 89 | numline = int(640 / eachsize) 90 | 91 | 缩小并拼接图片: 92 | ``` 93 | x = 0 94 | y = 0 95 | 96 | for i in pics: 97 | try: 98 | #打开图片 99 | img = Image.open(user + "/" + i) 100 | except IOError: 101 | print("Error: 没有找到文件或读取文件失败") 102 | else: 103 | #缩小图片 104 | img = img.resize((eachsize, eachsize), Image.ANTIALIAS) 105 | #拼接图片 106 | toImage.paste(img, (x * eachsize, y * eachsize)) 107 | x += 1 108 | if x == numline: 109 | x = 0 110 | y += 1 111 | ``` 112 | 保存图片到本地: 113 | 114 | toImage.save(user + ".jpg") 115 | 116 | 在微信的文件传输助手发合成后的图片给使用者: 117 | 118 | itchat.send_image(user + ".jpg", 'filehelper') 119 | 120 | ------ 121 | 122 | ###完整代码(下载本人[github项目][8]会更好点): 123 | ``` 124 | from numpy import * 125 | import itchat 126 | import urllib 127 | import requests 128 | import os 129 | 130 | import PIL.Image as Image 131 | from os import listdir 132 | import math 133 | 134 | itchat.auto_login(enableCmdQR=True) 135 | 136 | friends = itchat.get_friends(update=True)[0:] 137 | 138 | user = friends[0]["UserName"] 139 | 140 | print(user) 141 | 142 | os.mkdir(user) 143 | 144 | num = 0 145 | 146 | for i in friends: 147 | img = itchat.get_head_img(userName=i["UserName"]) 148 | fileImage = open(user + "/" + str(num) + ".jpg",'wb') 149 | fileImage.write(img) 150 | fileImage.close() 151 | num += 1 152 | 153 | pics = listdir(user) 154 | 155 | numPic = len(pics) 156 | 157 | print(numPic) 158 | 159 | eachsize = int(math.sqrt(float(640 * 640) / numPic)) 160 | 161 | print(eachsize) 162 | 163 | numline = int(640 / eachsize) 164 | 165 | toImage = Image.new('RGBA', (640, 640)) 166 | 167 | 168 | print(numline) 169 | 170 | x = 0 171 | y = 0 172 | 173 | for i in pics: 174 | try: 175 | #打开图片 176 | img = Image.open(user + "/" + i) 177 | except IOError: 178 | print("Error: 没有找到文件或读取文件失败") 179 | else: 180 | #缩小图片 181 | img = img.resize((eachsize, eachsize), Image.ANTIALIAS) 182 | #拼接图片 183 | toImage.paste(img, (x * eachsize, y * eachsize)) 184 | x += 1 185 | if x == numline: 186 | x = 0 187 | y += 1 188 | 189 | 190 | toImage.save(user + ".jpg") 191 | 192 | 193 | itchat.send_image(user + ".jpg", 'filehelper') 194 | 195 | 196 | 197 | 198 | ``` 199 | 200 | 201 | 202 | 203 | [1]: https://github.com/15331094/wxImage 204 | [2]: https://github.com/15331094/wxImage/blob/master/screenshots/@38d3133a3c5556b510cbe0d83557cfaf6923ede6845501b000c8ebef984cb68c.jpg?raw=true 205 | [3]: https://github.com/15331094/wxImage/blob/master/screenshots/@7464eb52a847b7cb7698f2f004586e9d22ed5d148a07da30386c2a726e900320.jpg?raw=true 206 | [4]: https://github.com/15331094/wxImage/blob/master/screenshots/@7c31f485fe852dad33336c52e10565aeab009477fdf0adf28838ca1d35e666da.jpg?raw=true 207 | [5]: https://github.com/15331094/wxImage/blob/master/screenshots/@f5c98c1d7b53eef38e4db58663ab5f3b93d49f333e81a61afc6c21b349a874f0.jpg?raw=true 208 | [6]: https://github.com/15331094/wxImage 209 | [7]: https://github.com/15331094/wxImage/blob/master/screenshots/wxid_eujni1y71a522_1489555725169_73.png?raw=true 210 | [8]: https://github.com/15331094/wxImage 211 | [9]: https://github.com/15331094/wxImage/blob/master/screenshots/360321781025374987.jpg?raw=true 212 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | asn1crypto==0.21.1 2 | attrs==16.3.0 3 | Automat==0.5.0 4 | beautifulsoup4==4.5.3 5 | bs4==0.0.1 6 | cffi==1.9.1 7 | constantly==15.1.0 8 | cryptography==1.8.1 9 | cssselect==1.0.1 10 | idna==2.5 11 | incremental==16.10.1 12 | itchat==1.2.30 13 | lxml==3.7.3 14 | numpy==1.12.0 15 | olefile==0.44 16 | packaging==16.8 17 | parsel==1.1.0 18 | Pillow==4.0.0 19 | pyasn1==0.2.3 20 | pyasn1-modules==0.0.8 21 | pycparser==2.17 22 | PyDispatcher==2.0.5 23 | pyOpenSSL==16.2.0 24 | pyparsing==2.2.0 25 | pypng==0.0.18 26 | PyQRCode==1.2.1 27 | pywin32==220 28 | queuelib==1.4.2 29 | requests==2.13.0 30 | Scrapy==1.3.3 31 | service-identity==16.0.0 32 | six==1.10.0 33 | Twisted==17.1.0 34 | w3lib==1.17.0 35 | zope.interface==4.3.3 36 | -------------------------------------------------------------------------------- /screenshots/360321781025374987.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzm1997/wxImage/e36f8d34ff96b0ef89745d139f8fb9f6172005c1/screenshots/360321781025374987.jpg -------------------------------------------------------------------------------- /screenshots/@7464eb52a847b7cb7698f2f004586e9d22ed5d148a07da30386c2a726e900320.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzm1997/wxImage/e36f8d34ff96b0ef89745d139f8fb9f6172005c1/screenshots/@7464eb52a847b7cb7698f2f004586e9d22ed5d148a07da30386c2a726e900320.jpg -------------------------------------------------------------------------------- /screenshots/@7c31f485fe852dad33336c52e10565aeab009477fdf0adf28838ca1d35e666da.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzm1997/wxImage/e36f8d34ff96b0ef89745d139f8fb9f6172005c1/screenshots/@7c31f485fe852dad33336c52e10565aeab009477fdf0adf28838ca1d35e666da.jpg -------------------------------------------------------------------------------- /screenshots/@f5c98c1d7b53eef38e4db58663ab5f3b93d49f333e81a61afc6c21b349a874f0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzm1997/wxImage/e36f8d34ff96b0ef89745d139f8fb9f6172005c1/screenshots/@f5c98c1d7b53eef38e4db58663ab5f3b93d49f333e81a61afc6c21b349a874f0.jpg -------------------------------------------------------------------------------- /screenshots/wxid_eujni1y71a522_1489555725169_73.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzm1997/wxImage/e36f8d34ff96b0ef89745d139f8fb9f6172005c1/screenshots/wxid_eujni1y71a522_1489555725169_73.png -------------------------------------------------------------------------------- /wxImage.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "原文地址:[itchat+pillow实现微信好友头像爬取和拼接](https://zhuanlan.zhihu.com/p/25782937?group_id=826931026017292288)" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "2.7.13 (default, Jan 19 2017, 14:48:08) \n", 20 | "[GCC 6.3.0 20170118]\n", 21 | "sys.version_info(major=2, minor=7, micro=13, releaselevel='final', serial=0)\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "#我的Python版本是:\n", 27 | "import sys \n", 28 | "print sys.version \n", 29 | "print sys.version_info " 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "这里我用Python2.7,实际上代码在Python3.6,3.5运行一切正常\n", 37 | "# 核心\n", 38 | "- itchat读取微信好友列表和头像\n", 39 | "- 用pillow拼接头像画图\n", 40 | "\n", 41 | "登录微信网页版,`itchat.auto_login()`会生成QR码,使用手机扫一扫即可登录。\n", 42 | "\n", 43 | "提示`Please scan the QR code to log in.`时,用手机扫描弹出的二维码\n", 44 | "\n", 45 | "[命令行二维码技巧:](https://itchat.readthedocs.io/zh/latest/#_4)\n", 46 | "1. 通过以下命令可以在登陆的时候使用命令行显示二维码:`itchat.auto_login(enableCmdQR=True)`\n", 47 | "2. 若背景色为浅色(白色),可以将enableCmdQR赋值为负值:`itchat.auto_login(enableCmdQR=-1)`\n" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 2, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "name": "stdout", 57 | "output_type": "stream", 58 | "text": [ 59 | "█\r" 60 | ] 61 | }, 62 | { 63 | "name": "stderr", 64 | "output_type": "stream", 65 | "text": [ 66 | "Getting uuid of QR code.\n", 67 | "Downloading QR code.\n", 68 | "Please scan the QR code to log in.\n", 69 | "Please press confirm on your phone.\n", 70 | "Loading the contact, this may take a little while.\n", 71 | "Login successfully as 小尧\n" 72 | ] 73 | } 74 | ], 75 | "source": [ 76 | "import itchat\n", 77 | "itchat.auto_login()" 78 | ] 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "metadata": {}, 83 | "source": [ 84 | "使用`friends`储存好友列表,`update=True`可以确保好友列表是最新的。注意好友列表第0个是自己" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": 3, 90 | "metadata": { 91 | "collapsed": true 92 | }, 93 | "outputs": [], 94 | "source": [ 95 | "friends = itchat.get_friends(update=True)[0:]" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "`friends`好友列表第0个是自己,我们可以看一下。顺带说一下,好友列表的顺序 **(貌似)** 是按照好友添加顺序" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 4, 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "data": { 112 | "text/plain": [ 113 | ", 'PYQuanPin': u'xiaoyao', 'RemarkPYInitial': u'', 'Uin': 31873955, 'AppAccountFlag': 0, 'VerifyFlag': 0, 'Province': u'\\u798f\\u5efa', 'KeyWord': u'Lai', 'RemarkName': u'', 'PYInitial': u'XY', 'ChatRoomId': 0, u'IsOwner': 0, 'HideInputBarFlag': 0, u'HeadImgFlag': 1, 'EncryChatRoomId': '', 'AttrStatus': 4318311, 'SnsFlag': 177, 'MemberCount': 0, u'WebWxPluginSwitch': 3, 'Alias': '', 'Signature': u'\\u68a6\\u60f3\\u8981\\u6709\\uff0c\\u8d76\\u8def\\u8981\\u7d27\\u3002\\u5927\\u7ea6\\u534a\\u5e74\\u53d1\\u4e00\\u6b21\\u670b\\u53cb\\u5708\\u3002', 'ContactFlag': 7, 'NickName': u'\\u5c0f\\u5c27', 'RemarkPYQuanPin': u'', 'HeadImgUrl': u'/cgi-bin/mmwebwx-bin/webwxgeticon?seq=621078783&username=@9eaa312b152f53c9898099716c11be4e&skey=@crypt_a6fa4b82_18f568cc5b6d337ca61905447f963a22', 'Sex': 1, 'StarFriend': 0, 'Statues': 0}>" 114 | ] 115 | }, 116 | "execution_count": 4, 117 | "metadata": {}, 118 | "output_type": "execute_result" 119 | } 120 | ], 121 | "source": [ 122 | "friends[0]" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "创建一个目录,用来保存所有好友头像。注意使用`os.chdir(user)`切换个到工作目录,方便后续保存图片。\n", 130 | "\n" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": null, 136 | "metadata": { 137 | "scrolled": true 138 | }, 139 | "outputs": [ 140 | { 141 | "name": "stdout", 142 | "output_type": "stream", 143 | "text": [ 144 | "xiaoyao\n" 145 | ] 146 | }, 147 | { 148 | "data": { 149 | "text/plain": [ 150 | "'/home/yao/\\xe6\\x96\\x87\\xe6\\xa1\\xa3/PythonApplication1/jupyter_notebook/xiaoyao'" 151 | ] 152 | }, 153 | "execution_count": 5, 154 | "metadata": {}, 155 | "output_type": "execute_result" 156 | } 157 | ], 158 | "source": [ 159 | "import os\n", 160 | "user = friends[0][\"PYQuanPin\"][0:]\n", 161 | "print(user)\n", 162 | "os.mkdir(user)\n", 163 | "os.chdir(user)\n", 164 | "os.getcwd()" 165 | ] 166 | }, 167 | { 168 | "cell_type": "markdown", 169 | "metadata": {}, 170 | "source": [ 171 | "批量下载好友头像,储存到`friends[i]['img']`中。然后我们`print(friends[0]`看看有没有变化(正常情况下应该可以看到增加了img,以二进制方式储存头像)。因为我家网络经常链接失败,所以用`try...except...`来写这一段。\n", 172 | "\n", 173 | "`\"UserName\"`这个字段开头总有个@符号,直接暴力去除。如果不喜欢的话,可以把`\"UserName\"`换成`\"PYQuanPin\"`,不过不保证重名。\n", 174 | "\n", 175 | "同时把头像保存在`user`目录地下,方便下一步使用。\n", 176 | "\n", 177 | "### python使用open经常报错:TypeError: an integer is required的解决方案\n", 178 | "错误是由于从os模块引入了所有的函数导致的,os模块下有一个open函数,接受整型的文件描述符和打开模式,`from os import *`引入os模块的open函数,覆盖了python内建的open函数,导致错误。删除`from os import *`这行,然后再根据需要,指定引入os模块下的函数" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": null, 184 | "metadata": { 185 | "scrolled": true 186 | }, 187 | "outputs": [], 188 | "source": [ 189 | "for i in friends:\n", 190 | " try:\n", 191 | " i['img'] = itchat.get_head_img(userName=i[\"UserName\"])\n", 192 | " i['ImgName']=i[\"UserName\"][1:] + \".jpg\"\n", 193 | " except ConnectionError:\n", 194 | " print('get '+i[\"UserName\"][1:]+' fail')\n", 195 | " fileImage=open(i['ImgName'],'wb')\n", 196 | " fileImage.write(i['img'])\n", 197 | " fileImage.close()\n", 198 | "#这里不建议看friends[0],太长了" 199 | ] 200 | }, 201 | { 202 | "cell_type": "markdown", 203 | "metadata": {}, 204 | "source": [ 205 | "看看我有多少个好友(friends里面有多少条记录),看看下载了多少头像(`os.listdir(os.getcwd())`看目录底下有多少文件)" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": null, 211 | "metadata": {}, 212 | "outputs": [], 213 | "source": [ 214 | "friendsSum=len(friends)\n", 215 | "imgList=os.listdir(os.getcwd())\n", 216 | "numImages=len(imgList)\n", 217 | "print('I have ',friendsSum,'friend(s), and I got ',numImages,'image(s)')" 218 | ] 219 | }, 220 | { 221 | "cell_type": "markdown", 222 | "metadata": {}, 223 | "source": [ 224 | "单个图像边长`eachsize=64`像素,一行`eachline=int(sqrt(numImages))+1`个头像,最终图像边长`eachSize*eachline`" 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": null, 230 | "metadata": {}, 231 | "outputs": [], 232 | "source": [ 233 | "import math\n", 234 | "eachSize=64\n", 235 | "eachLine=int(math.sqrt(numImages))+1\n", 236 | "print(\"单个图像边长\",eachSize,\"像素,一行\",eachLine,\"个头像,最终图像边长\",eachSize*eachLine)" 237 | ] 238 | }, 239 | { 240 | "cell_type": "markdown", 241 | "metadata": {}, 242 | "source": [ 243 | "import图像处理Python图像处理库:PIL中Image\n", 244 | "1. 新建一块画布\n", 245 | "2. 在坐标(0,0)位置放上第一个人头像\n", 246 | "3. 向右平移坐标" 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": null, 252 | "metadata": {}, 253 | "outputs": [], 254 | "source": [ 255 | "import PIL.Image as Image\n", 256 | "toImage = Image.new('RGBA', (eachSize*eachLine,eachSize*eachLine))#新建一块画布\n", 257 | "x = 0\n", 258 | "y = 0\n", 259 | "for i in imgList:\n", 260 | " try:\n", 261 | " img = Image.open(i)#打开图片\n", 262 | " except IOError:\n", 263 | " print(\"Error: 没有找到文件或读取文件失败\",i)\n", 264 | " else:\n", 265 | " img = img.resize((eachSize, eachSize), Image.ANTIALIAS)#缩小图片\n", 266 | " toImage.paste(img, (x * eachSize, y * eachSize))#拼接图片\n", 267 | " x += 1\n", 268 | " if x == eachLine:\n", 269 | " x = 0\n", 270 | " y += 1\n", 271 | "print(\"图像拼接完成\")" 272 | ] 273 | }, 274 | { 275 | "cell_type": "markdown", 276 | "metadata": {}, 277 | "source": [ 278 | "看一下拼接好的图像是什么样的(注意文件过大是常有的现象,请先去掉注释)\n", 279 | "回到上一级目录(没人想在一堆文件里面找拼图吧?)\n", 280 | "然后保存文件,顺带发送给文件传输助手" 281 | ] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": null, 286 | "metadata": {}, 287 | "outputs": [], 288 | "source": [ 289 | "#toImage.show()\n", 290 | "\n", 291 | "os.chdir(os.path.pardir)\n", 292 | "os.getcwd()\n", 293 | "\n", 294 | "toImage.save(friends[0][\"PYQuanPin\"][0:]+\".jpg\")\n", 295 | "itchat.send_image(friends[0][\"PYQuanPin\"][0:]+\".jpg\", 'filehelper')" 296 | ] 297 | }, 298 | { 299 | "cell_type": "markdown", 300 | "metadata": {}, 301 | "source": [ 302 | "至此,大功告成,别忘记退出网页版微信" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": null, 308 | "metadata": {}, 309 | "outputs": [], 310 | "source": [ 311 | "itchat.logout()" 312 | ] 313 | } 314 | ], 315 | "metadata": { 316 | "kernelspec": { 317 | "display_name": "Python 3", 318 | "language": "python", 319 | "name": "python3" 320 | }, 321 | "language_info": { 322 | "codemirror_mode": { 323 | "name": "ipython", 324 | "version": 2 325 | }, 326 | "file_extension": ".py", 327 | "mimetype": "text/x-python", 328 | "name": "python", 329 | "nbconvert_exporter": "python", 330 | "pygments_lexer": "ipython2", 331 | "version": "2.7.13" 332 | } 333 | }, 334 | "nbformat": 4, 335 | "nbformat_minor": 2 336 | } 337 | -------------------------------------------------------------------------------- /wxImage.py: -------------------------------------------------------------------------------- 1 | import itchat 2 | import os 3 | 4 | import PIL.Image as Image 5 | from os import listdir 6 | import math 7 | 8 | itchat.auto_login(enableCmdQR=True) 9 | 10 | friends = itchat.get_friends(update=True)[0:] 11 | 12 | user = friends[0]["UserName"] 13 | 14 | print(user) 15 | 16 | os.mkdir(user) 17 | 18 | num = 0 19 | 20 | for i in friends: 21 | img = itchat.get_head_img(userName=i["UserName"]) 22 | fileImage = open(user + "/" + str(num) + ".jpg",'wb') 23 | fileImage.write(img) 24 | fileImage.close() 25 | num += 1 26 | 27 | pics = listdir(user) 28 | 29 | numPic = len(pics) 30 | 31 | print(numPic) 32 | 33 | eachsize = int(math.sqrt(float(640 * 640) / numPic)) 34 | 35 | print(eachsize) 36 | 37 | numline = int(640 / eachsize) 38 | 39 | toImage = Image.new('RGBA', (640, 640)) 40 | 41 | 42 | print(numline) 43 | 44 | x = 0 45 | y = 0 46 | 47 | for i in pics: 48 | try: 49 | #打开图片 50 | img = Image.open(user + "/" + i) 51 | except IOError: 52 | print("Error: 没有找到文件或读取文件失败") 53 | else: 54 | #缩小图片 55 | img = img.resize((eachsize, eachsize), Image.ANTIALIAS) 56 | #拼接图片 57 | toImage.paste(img, (x * eachsize, y * eachsize)) 58 | x += 1 59 | if x == numline: 60 | x = 0 61 | y += 1 62 | 63 | 64 | toImage.save(user + ".jpg") 65 | 66 | 67 | itchat.send_image(user + ".jpg", 'filehelper') 68 | 69 | 70 | --------------------------------------------------------------------------------