月底已至,突然想起我文章的头图还没搞,恰好阿里云的流量还有剩,正好爬下wallheaven的图片来做头图。
import time
import requests
import os
lst = []
def get_pic_url():
for i in range(1,3):
url = 'https://wallhaven.cc/api/v1/search?q=id%3A1&categories=110&purity=100&atleast=1280x720&ratios=landscape&topRange=1y&sorting=toplist&order=desc&ai_art_filter=1&page={0}'.format(i)
response = requests.get(url)
# 解析json数据
data = response.json()
# 获取图片地址
for i in data['data']:
lst.append(i['path'])
txt = open('pic_url.txt','w')
txt.write(str(lst))
txt.close()
def download_pic():
index = 100001
for i in lst:
response = requests.get(i)
ex = i.split('.')[-1]
# 下载图片
try:
os.mkdir('pic')
except:
pass
with open('pic/{0}.{1}'.format(index,ex), 'wb') as f:
f.write(response.content)
print('下载第{0}张图片'.format(index))
time.sleep(1)
index = index + 1
get_pic_url()
download_pic()
wallheaven的图片质量虽好,但是动辄10m上下的大小实在令人有些无从下手。。。😅
以前我也使用过类似 TinyPNG 的在线图片压缩服务,但是唯一一点不好的就是无法脱离他在线的服务,需要把照片上传到他的网站上才能进行压缩。于是所以就找了一下能在 Debian 上使用的工具,然后发现了 jpegoptim 。
安装
apt-get install jpegoptim
基本使用
# 直接使用 jpegoptim file.jpg
# 使用正则读取文件夹下的jpg,并把大小压缩到1000k左右,输出到./compressed文件夹下
find . -name "*.jpg" | xargs jpegoptim --size=1000k -d ./compressed -p
``