微信好友頭像全家福詳細教程python版

昨天按照大佬的代碼,做了一個微信全家福的圖片,后面好多人問我是怎么做的,索性我就出個詳細的教程吧,我python也是三腳貓功夫,有不對的地方,還請各位大佬手下留情。

先看看成果吧

微信好友頭像全家福詳細教程

既然是用python來實現的,那么就需要安裝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、方法

  • 首先到https://pypi.python.org/pypi/pip#downloads 下載pip包

  • 解壓到一個文件夾,用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>

微信好友頭像全家福詳細教程
微信好友頭像全家福詳細教程
微信好友頭像全家福詳細教程
微信好友頭像全家福詳細教程
微信好友頭像全家福詳細教程

代碼也就兩個地方需要修改,改好直接運行就行了

微信好友頭像全家福詳細教程
微信好友頭像全家福詳細教程
微信好友頭像全家福詳細教程
微信好友頭像全家福詳細教程
微信好友頭像全家福詳細教程
微信好友頭像全家福詳細教程

差不多就是這樣。

源代碼源自簡書作者[羅羅攀].

原文地址:https://blog.ccswust.org/7571.html

?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,702評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,143評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,553評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,620評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,416評論 6 405
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,940評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,024評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,170評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,709評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,597評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,784評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,291評論 5 357
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,029評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,407評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,663評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,403評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,746評論 2 370

推薦閱讀更多精彩內容