Python 將 QQ 好友頭像生成祝福語的實現(xiàn)代碼
本文我們來看一下如何使用 Python 將 QQ 好友頭像拼成“五一快樂”四個字。我們可以將整個實現(xiàn)過程分為兩步:爬取 QQ 好友頭像、利用好友頭像生成文字。
爬取頭像
爬取 QQ 好友頭像我們需要借助于 QQ 郵箱,首先我們從瀏覽器上登錄 QQ 郵箱,之后按 F12
鍵打開開發(fā)者工具并用鼠標選中 Network
選項,如下圖所示:
再接著我們按 F5
鍵刷新一下網(wǎng)頁,然后在 Filter
中輸入 laddr_lastlist
,如下圖所示:
我們再點 Name
下的鏈接,點擊之后右側(cè)會出現(xiàn)一個窗口,我們用鼠標選中 Response
項,如下圖所示:
我們最后將 Response
下面出現(xiàn)的內(nèi)容復制到 txt 文件。
獲取了爬取需要用到的東西后我們就可以開始實現(xiàn)爬取了,我們使用 requests
庫將頭像圖片爬取來下存到本地,代碼實現(xiàn)如下所示:
# 獲取頭像 def get_head(): file = codecs.open('qqfriends.txt', 'rb', 'utf-8') s = file.read() pattern = re.compile(r'\d+@qq.com') # 正則表達式匹配所有的 qq 號 all_mail = pattern.findall(s) # 用于存儲需要訪問的鏈接 all_link = [] url = 'http://qlogo.store.qq.com/qzone/' for mail in all_mail: qq = mail.replace('@qq.com', '') l = url + qq + '/' + qq + '/100' all_link.append(l) # 初始化下載圖片數(shù)量 i = 0 # 獲取朋友頭像數(shù)量 friends_count = len(all_link) print('共{}個頭像'.format(friends_count)) # 遍歷鏈接,下載頭像 for link in all_link: i += 1 saveurl = 'head/' + str(i) + '.png' print('第 %d 個' % i, end=' ') sava2img(link, saveurl) return True # 存儲圖片函數(shù),picurl 是圖片的 URL,saveurl 是本地存儲位置 def sava2img(picurl, saveurl): try: start = time.time() response = requests.get(picurl, stream=True) # 下載圖片到本地 with open(saveurl, 'wb') as file: file.write(response.content) print('下載完成...', end=' ') end = time.time() time_ = end - start print('用時: %.2f秒' % (time_)) return True except: print('出錯了...')
生成文字
現(xiàn)在 QQ 頭像圖片已經(jīng)有了,我們再看一下如何用這些圖片生成文字,這里需要用到一下第三方庫 PIL
,安裝使用 pip install Pillow
,我們需要先將 “五一快樂” 四個字轉(zhuǎn)化為漢字庫的點陣數(shù)據(jù)再使用,現(xiàn)在看一下具體實現(xiàn):
# 將字轉(zhuǎn)化為漢字庫的點陣數(shù)據(jù) def char2bit(textStr): KEYS = [0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01] target = [] global count count = 0 for x in range(len(textStr)): text = textStr[x] rect_list = [] * 16 for i in range(16): rect_list.append([] * 16) gb2312 = text.encode('gb2312') hex_str = binascii.b2a_hex(gb2312) result = str(hex_str, encoding='utf-8') area = eval('0x' + result[:2]) - 0xA0 index = eval('0x' + result[2:]) - 0xA0 offset = (94 * (area-1) + (index-1)) * 32 font_rect = None with open("HZK16", "rb") as f: f.seek(offset) font_rect = f.read(32) for k in range(len(font_rect) // 2): row_list = rect_list[k] for j in range(2): for i in range(8): asc = font_rect[k * 2 + j] flag = asc & KEYS[i] row_list.append(flag) output = [] for row in rect_list: for i in row: if i: output.append('1') count+=1 else: output.append('0') target.append(''.join(output)) return target # 生成圖片文字 def head2char(workspace,folder,self,outlist): # 將工作路徑轉(zhuǎn)移至頭像文件夾 os.chdir(folder) # 獲取文件夾內(nèi)頭像列表 imgList = os.listdir(folder) # 獲取頭像圖片個數(shù) numImages = len(imgList) # 設置頭像裁剪后尺寸 eachSize = 100 # 變量 n 用于循環(huán)遍歷頭像圖片 n=0 # 變量 count 用于為最終生成的單字圖片編號 count = 0 # 初始化顏色列表,用于背景著色 colorlist = ['#FFFACD','#F0FFFF','#BFEFFF','#b7facd','#ffe7cc','#fbccff','#d1ffb8','#febec0','#E0EEE0'] # index 用來改變不同字的背景顏色 index = 0 # 每個 item 對應不同字的點陣信息 for item in outlist: # 將工作路徑轉(zhuǎn)到頭像所在文件夾 os.chdir(folder) # 新建一個帶有背景色的畫布,16 * 16點陣,每個點處填充 2 * 2 張頭像圖片,故長為 16 * 2 * 100 canvas = Image.new('RGB', (3200, 3200), colorlist[index]) # 新建一塊畫布 # index 變換,用于變換背景顏色 index = (index+1)%9 count += 1 # 每個 16 * 16 點陣中的點,用四張 100 * 100 的頭像來填充 for i in range(16*16): # 點陣信息為 1,即代表此處要顯示頭像來組字 if item[i] == "1": # 循環(huán)讀取連續(xù)的四張頭像圖片 x1 = n % len(imgList) x2 = (n+1) % len(imgList) x3 = (n+2) % len(imgList) x4 = (n+3) % len(imgList) # 以下四組 try,將讀取到的四張頭像填充到畫板上對應的一個點位置 # 點陣處左上角圖片 1/4 try: # 打開圖片 img = Image.open(imgList[x1]) except IOError: print("有1張圖片讀取失敗,已使用備用圖像替代") img = Image.open(self) finally: # 縮小圖片 img = img.resize((eachSize, eachSize), Image.ANTIALIAS) # 拼接圖片 canvas.paste(img, ((i % 16) * 2 * eachSize, (i // 16) * 2 * eachSize)) # 點陣處右上角圖片 2/4 try: img = Image.open(imgList[x2]) except IOError: print("有1張圖片讀取失敗,已使用備用圖像替代") img = Image.open(self) finally: img = img.resize((eachSize, eachSize), Image.ANTIALIAS) canvas.paste(img, (((i % 16) * 2 + 1) * eachSize, (i // 16) * 2 * eachSize)) # 點陣處左下角圖片 3/4 try: img = Image.open(imgList[x3]) except IOError: print("有1張圖片讀取失敗,已使用備用圖像替代") img = Image.open(self) finally: img = img.resize((eachSize, eachSize), Image.ANTIALIAS) canvas.paste(img, ((i % 16) * 2 * eachSize, ((i // 16) * 2 + 1 ) * eachSize)) # 點陣處右下角圖片 4/4 try: img = Image.open(imgList[x4]) except IOError: print("有1張圖片讀取失敗,已使用備用圖像替代") img = Image.open(self) finally: img = img.resize((eachSize, eachSize), Image.ANTIALIAS) canvas.paste(img, (((i % 16) * 2 + 1) * eachSize, ((i // 16) * 2 + 1) * eachSize)) #調(diào)整 n 以讀取后續(xù)圖片 n= (n+4) % len(imgList) os.chdir(workspace) # 創(chuàng)建文件夾用于存儲輸出結(jié)果 if not os.path.exists('output'): os.mkdir('output') os.chdir('output') # 存儲將拼接后的圖片,quality 為圖片質(zhì)量,1 - 100,100 最高 canvas.save('result%d.jpg'% count, quality=100)
看一下實現(xiàn)效果:
到此這篇關(guān)于用 Python 將 QQ 好友頭像生成祝福語的文章就介紹到這了,更多相關(guān)python qq好友祝福語內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python中torch.load中的map_location參數(shù)使用
在PyTorch中,torch.load()函數(shù)是用于加載保存模型或張量數(shù)據(jù)的重要工具,map_location參數(shù)為我們提供了極大的靈活性,具有一定的參考價值,感興趣的可以了解一下2024-03-03Sphinx環(huán)境配置及VScode編寫Rst文檔轉(zhuǎn)html的步驟
sphinx主要用于編寫 reStructuredText 和 Markdown 格式技術(shù)文檔,編寫此類技術(shù)文檔時Sphinx工具可將其轉(zhuǎn)為html、pdf、ePub等格式,這篇文章主要介紹了Sphinx環(huán)境配置及VScode編寫Rst文檔轉(zhuǎn)html,需要的朋友可以參考下2023-03-03python3模擬實現(xiàn)xshell遠程執(zhí)行l(wèi)inux命令的方法
今天小編就為大家分享一篇python3模擬實現(xiàn)xshell遠程執(zhí)行l(wèi)inux命令的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07Python環(huán)境搭建過程從安裝到Hello World
這篇文章主要介紹了Python環(huán)境搭建過程從安裝到Hello World,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02