昨天按照大佬的代碼,做了一個微信全家福的圖片,后面好多人問我是怎么做的,索性我就出個詳細的教程吧,我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>
代碼也就兩個地方需要修改,改好直接運行就行了
差不多就是這樣。
源代碼源自簡書作者[羅羅攀].