Python "520+1" photo wall
How did this article come from? That's a long story, but the motivation is for those who like it. Just after 520, take it out and share it. I hope to give you some inspiration if you are looking for how to move him!
#首先导入模块
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() #然后生成照片墙
The crawler used here crawls Baidu pictures. If you need to customize it yourself, you can refer to my previous blog.
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=\
©right=&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)
The code is posted directly here, and I will not explain it, because it is something that I have done before, which is the code of a Baidu image crawler.
The highlight-the most critical part is here. How to generate this photo wall and how to layout it?
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()#展示图片墙
You can change the graphic matrix figure and various parameters according to your needs.
Post a few finished pictures: I
heard that only those who like it can generate a beautiful photo wall! Maybe the official account has been watched too much, and the words have an internal taste...