昨天按照大佬的代码,做了一个微信全家福的图片,后面好多人问我是怎么做的,索性我就出个详细的教程吧,我python也是三脚猫功夫,有不对的地方,还请各位大佬手下留情。
先看看成果吧

<pre class="prettyprint" style="box-sizing: border-box; overflow: auto; font: 400 12px/20px "courier new"; display: block; padding: 10px 15px; margin: 20px 0px; color: rgb(248, 248, 212); word-break: break-all; overflow-wrap: break-word; background: rgb(39, 40, 34); border: 0px; border-radius: 4px; max-height: 637px; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">我的环境是 win10 64位 python 3.7</pre>
这是我的安装目录

怎么安装python?
可以通过python官网下载:https://www.python.org/
我后面把这次试验的软件和运行库都打包了的





备注:如果通过python-V没有检查到版本,那基本是没有做环境变量
win10下python环境变量设置
安装之后配置环境变量的步骤如下:
1,点“我的电脑”,右键选“属性”
2,选择“高级系统设置”—>选“环境变量”—>在“系统变量”中选中“Path”,再点“编辑”—>再点“编辑文本”
3,在“变量值”一栏,把自己所安装的python路径拷进去就可以了,我安装的路径是“C:[Python3.7](https://blog.ccswust.org/tag/python3-7 "View all posts in Python3.7")”
注意:在拷贝路径“C:[Python3.7](https://blog.ccswust.org/tag/python3-7 "View all posts in Python3.7")”时,前面要加分号
<pre class="prettyprint" style="box-sizing: border-box; overflow: auto; font: 400 12px/20px "courier new"; display: block; padding: 10px 15px; margin: 20px 0px; color: rgb(248, 248, 212); word-break: break-all; overflow-wrap: break-word; background: rgb(39, 40, 34); border: 0px; border-radius: 4px; max-height: 637px; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">;</pre>
还要注意,分号一定是英文输入法里的分号,我刚开始没有注意到这一点,导致在命令行里输入python命令时,总是失败,会提示‘python’不是内部或外部命令,也不是可运行的程序或批处理文件





若遇到“’pip’ 不是内部或外部命令,也不是可运行的程序或批处理文件时”
<pre class="prettyprint" style="box-sizing: border-box; overflow: auto; font: 400 12px/20px "courier new"; display: block; padding: 10px 15px; margin: 20px 0px; color: rgb(248, 248, 212); word-break: break-all; overflow-wrap: break-word; background: rgb(39, 40, 34); border: 0px; border-radius: 4px; max-height: 637px; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">python: can't open file 'pip': [Errno 2] No such file or directory</pre>
我看了下site-packages的pip和pip-1.2.1-py2.7.egg-info中,都没有pip.py或pip.exe之类的。
并且,此路径也没加到PATH中去,所以没法运行,也是正常的。
但是为何官网教程,竟然会这么写,很是奇怪。
解决方法:添加对应的环境变量
1、问题
cmd进去输入
<pre class="prettyprint" style="box-sizing: border-box; overflow: auto; font: 400 12px/20px "courier new"; display: block; padding: 10px 15px; margin: 20px 0px; color: rgb(248, 248, 212); word-break: break-all; overflow-wrap: break-word; background: rgb(39, 40, 34); border: 0px; border-radius: 4px; max-height: 637px; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">pip install itchat</pre>
提示我:’pip’ 不是内部或外部命令,也不是可运行的程序。
2、原因
问题的原因是我本机没有安装pip
3、方法
-
解压到一个文件夹,用CMD控制台进入解压目录,输入:
<pre class="prettyprint" style="box-sizing: border-box; overflow: auto; font: 12px/20px "courier new"; display: block; padding: 10px 15px; margin: 20px 0px; color: rgb(248, 248, 212); word-break: break-all; overflow-wrap: break-word; background: rgb(39, 40, 34); border: 0px; border-radius: 4px; max-height: 637px;">python setup.py install</pre>
-
执行完之后再输入:
<pre class="prettyprint" style="box-sizing: border-box; overflow: auto; font: 12px/20px "courier new"; display: block; padding: 10px 15px; margin: 20px 0px; color: rgb(248, 248, 212); word-break: break-all; overflow-wrap: break-word; background: rgb(39, 40, 34); border: 0px; border-radius: 4px; max-height: 637px;">pip install itchat 以及</pre>
<pre class="prettyprint" style="box-sizing: border-box; overflow: auto; font: 12px/20px "courier new"; display: block; padding: 10px 15px; margin: 20px 0px; color: rgb(248, 248, 212); word-break: break-all; overflow-wrap: break-word; background: rgb(39, 40, 34); border: 0px; border-radius: 4px; max-height: 637px;">pip install pillow</pre>

【总结】
无论何时,在cmd中出现了:
不是内部或外部命令,也不是可运行的程序或批处理文件。
的错误,那都是没有找到对应的可执行程序,所以:
要么是本身系统中的确没有此种可执行程序 -> 下载或安装此种程序,再记得确保PATH中包含对应路径。
要么是有此程序,但是PATH中没有包含对应的路径 -> 添加对应的路径到PATH中即可。
到这一步,环境差不多就配置OK了,接下来修改代码,我再贴下代码吧
<pre class="prettyprint" style="box-sizing: border-box; overflow: auto; font: 400 12px/20px "courier new"; display: block; padding: 10px 15px; margin: 20px 0px; color: rgb(248, 248, 212); word-break: break-all; overflow-wrap: break-word; background: rgb(39, 40, 34); border: 0px; border-radius: 4px; max-height: 637px; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 导入itchat用于登录微信,导入math用于计算图片大小 # 导入PIL用于图片修剪和拼接, 导入os用于获取下载的图片信息 import itchat import math import PIL.Image as Image import os # 修改filepath为指定的目录 filepath = "C:\Python3.7\img" # 登录,如果你是Windows,用下面这行代码,然后注释Linux itchat.auto_login() # 如果你用的是Linux,用下面这行代码,然后注释Windows # itchat.auto_login(enableCmdQR=1) # 读取好友 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(filepath + "/" + str(num) + ".jpg","wb") fileImage.write(img) fileImage.close() num += 1 # 利用os.listdir从文件路径中读取存放的图片 ls = os.listdir(filepath) # 根据分辨率,定义图片大小 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(filepath + "/" + 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(filepath + "/" + "all.jpg") # 用文件助手发送到手机微信端 itchat.send_image(filepath +"/"+"all.jpg","filehelper")</pre>





代码也就两个地方需要修改,改好直接运行就行了






差不多就是这样。
源代码源自简书作者[罗罗攀].
网友评论