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,各类参数
贴出几张成图吧:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
听说只有点了赞的人才能生成好看的照片墙哦!可能公众号看多了,说起话来都有内味了……

猜你喜欢

转载自blog.csdn.net/weixin_43594279/article/details/106269059