Python “多愛你一點”照片牆

Python “520+1”的照片牆

這篇文章是怎麼來的呢,那就說來話長了,不過動機就是給喜歡的ta的。剛過520就拿出來分享一下。希望給在找如何給ta製造感動的你一下靈感吧!

#首先導入模塊
import random		
import PIL.Image	#用來讀取圖片,需要額外下載
import os
import requests		#爲了寫爬蟲而準備的
import json
if __name__ == '__main__':
"""我們要將爬蟲爬到的圖片存在這個文件夾裏,這裏先創建文件夾,方面後面函數調用時不會報錯"""
    os.makedirs('祝你快樂/', exist_ok=True)
"""這裏的例子是以seventeen韓國男團爲對象進行照片爬取"""
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) \
        Chrome/70.0.3538.102 Safari/537.36 Edge/18.18362',
        'Referer': 'https://www.baidu.com/?tn=18029102_3_dg'
    }#這裏的headers請自行更改
    print('數據獲取需要時間,請耐心等待……')
    pictureSpider()#先爬取圖片
    happyGirl()	#然後生成照片牆

這裏用到的爬蟲是爬取百度圖片的,如有需要自行定製,可參考我之前的博客。

def downloads(url):
    name = url.split('/')[-1]
    response = requests.get(url, headers=headers)
    with open('祝你快樂/{}'.format(name), 'wb')as f:
        f.write(response.content)
    # print(url+'下載成功')


def get_detail_url():
    pic_urls = []
    for i in range(31):
        index = i * 30
        if index == 180:
            continue
        url = 'http://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592\
&is=&fp=result&queryWord=seventeen&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=&z=&ic=&hd=&latest=\
&copyright=&word=seventeen&s=&se=&tab=&width=&height=&face=&istype=&qc=&nc=1&fr=&expermode=&force=&pn={}&rn=30'.format(
            index)
        try:
            response = requests.get(url, headers=headers)
            if len(response.text) > 5000:
                data = json.loads(response.text, strict=False)
                thumb_urls = []
                for i in range(6):
                    thumb_url = data['data'][i]['thumbURL']
                    thumb_urls.append(thumb_url)
                pic_urls.extend(thumb_urls)
            else:
                continue
        except:
            continue
    return pic_urls


def pictureSpider():
    pic_urls = get_detail_url()
    for url in pic_urls:
        downloads(url)

這裏直接貼代碼,我也不解釋,因爲是之前做過的東西,就是一個百度圖片爬蟲的代碼。
重頭戲——最關鍵的部分來了,如何生成這個照片牆呢,怎麼佈局呢?

def happyGirl():
    # 定義圖形——這個圖形實際上就是一個愛心形
    figure = [
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    ]

    # 圖片尺寸 像素
    PIC_WIDTH, PIC_HEIGHT = 100, 100
    # 行數  列數
    row_num, column_num = len(figure), len(figure[0])
    # 讀取照片名 
    image_names = os.listdir("祝你快樂/")
    # 背景讀取 設置尺寸
    img = PIL.Image.open("祝你快樂/" + random.choice(image_names)).resize((column_num * PIC_WIDTH, row_num * PIC_HEIGHT))
    #其中random.choice就是用來隨機選擇圖片作爲背景圖的
    
    #print(img, image_names)
    for row in range(row_num):
        for column in range(column_num):
            if figure[row][column]:
                pic = PIL.Image.open("祝你快樂/" + random.choice(image_names)).resize((PIC_WIDTH, PIC_HEIGHT))
                img.paste(pic, (PIC_WIDTH * column, PIC_HEIGHT * row))
				#在指定的位置粘貼上圖片
    img.save("祝你快樂/like.png")#保存圖片牆
    img.show()#展示圖片牆

你可以根據自己的需要更改圖形矩陣figure,各類參數
貼出幾張成圖吧:

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
聽說只有點了讚的人才能生成好看的照片牆哦!可能公衆號看多了,說起話來都有內味了……

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章