水印添加
模块:
? ? ? ? 图像处理:Image
? ? ? ? 爬虫:lxml
代码时间
# 导入模块
from PIL import Image
# 获取 logo 图片
logo = Image.open('logo.png')
# 获取待添加水印 图片
img = Image.open('1.jpg')
# 将水印图添加到待添加图片上
# paste(添加的水印图片,(x,y)mask=可选掩码图像)
# 每张图片的尺寸都不一样,我这里的位置只做参考
img.paste(logo, (0, 625), mask=logo)
# 保存新图片
img.save('1.png')
小爬虫
import requests
from lxml import etree
# 请求函数
def get_html(url):
response = requests.get(url=url)
return response
# 主程序
def get_requests():
i = 0
url = 'https://pic.netbian.com/4kdongman/'
response = get_html(url).text
HTML = etree.HTML(response)
# 获取详情页
details = HTML.xpath('//ul[@class="clearfix"]/li/a/@href')
# 拼接详情页
details_url = ['https://pic.netbian.com' + i for i in details]
for url in details_url:
i += 1
response = get_html(url).text
HTML = etree.HTML(response)
# 获取图片地址
image_id = HTML.xpath('//a[@id="img"]/img/@src')[0]
# 拼接图片地址
url = 'https://pic.netbian.com' + image_id
response = get_html(url).content
print(f'正在下载第:{i} 张')
# 保存图片
with open(str(i) + '.jpg', 'wb') as f:
f.write(response)
print(f'第:{i} 下载完成...')
if __name__ == '__main__':
get_requests()
效果图:
|