当前位置: 首页 > news >正文

基于图像链接的批量下载

1. 获取图像路径

1.1 给定图像链接,这是一张图像

image_url = “https://univs-news-1256833609.cos.ap-beijing.myqcloud.com/123/upload/resources/image/7467914.jpg”

1.1通过网站规则得到其想要的图像链接

image_urls = [
f"https://univs-news-1256833609.cos.ap-beijing.myqcloud.com/123/upload/resources/image/{7467914 + i*2}.jpg" for i in range(50)
]

1.2 获取传入的参数,如果传入的图像路径参数是空,则判断是否使用默认的配置(非指定想要的图像链接)

if opt.image_urls:image_urls = opt.image_urls
else:while True:input_id = int(input("The following options:\n [0]: stop running. \n [1]: use default config.\nPlease enter the correct option: "))if input_id == 0:sys.exit()  # 退出程序# breakelif input_id == 1:image_urls = [f"https://univs-news-1256833609.cos.ap-beijing.myqcloud.com/123/upload/resources/image/{7467914 + i * 2}.jpg"for i in range(50)]break  # 输入正确,跳出循环else:print("input number is wrong,please input correct.\n")

2. 通过图像路径,获取图像,并下载到本地

# 遍历下载图像id = 1for i, image_url in enumerate(image_urls):# 发起HTTP请求获取图像数据response = requests.get(image_url)if response.status_code == 200:# 将图像数据转换为PIL Image对象image_data = BytesIO(response.content)image = PILImage.open(image_data)# 可以将 image 用于展示、保存或进一步处理image.save(f"./{cache_path}/{id}_{image_url.rsplit('/', 1)[1][:-4]}.png")  # 保存图像id += 1else:# 请求不到的数据进行错误返回print(f"Failed to download image: {i}_{image_url}")

3. 完整的代码

import os
import shutil
import sys
import argparse
import requests
from PIL import Image as PILImage
from io import BytesIOdef get_opt():"""参数设置"""parse = argparse.ArgumentParser()parse.add_argument("--cache_path", type=str, default="./tmp/data", help="缓存文件路径")parse.add_argument("--image_urls", type=list, default=[], help="请求的图像链接")opt = parse.parse_args()return optdef main():# 获取配置信息opt = get_opt()# 本地缓存路径cache_path = opt.cache_path# 清除现有的缓存文件夹(如果有的话)try:shutil.rmtree(cache_path)except:pass# 创建缓存文件夹os.makedirs(cache_path)# 获取图像链接if opt.image_urls:image_urls = opt.image_urlselse:while True:input_id = int(input("The following options:\n [0]: stop running. \n [1]: use default config.\nPlease enter the correct option: "))if input_id == 0:sys.exit()  # 退出程序# breakelif input_id == 1:image_urls = [f"https://univs-news-1256833609.cos.ap-beijing.myqcloud.com/123/upload/resources/image/{7467914 + i * 2}.jpg"for i in range(50)]break  # 输入正确,跳出循环else:print("input number is wrong,please input correct.\n")# 遍历下载图像id = 1for i, image_url in enumerate(image_urls):# 发起HTTP请求获取图像数据response = requests.get(image_url)if response.status_code == 200:# 将图像数据转换为PIL Image对象image_data = BytesIO(response.content)image = PILImage.open(image_data)# 可以将 image 用于展示、保存或进一步处理image.save(f"./{cache_path}/{id}_{image_url.rsplit('/', 1)[1][:-4]}.png")  # 保存图像id += 1else:# 请求不到的数据进行错误返回print(f"Failed to download image: {i}_{image_url}")if __name__ == '__main__':main()
http://www.lryc.cn/news/133409.html

相关文章:

  • mongodb使用心得
  • 学习Vue:响应式原理与性能优化策略
  • 神经网络基础-神经网络补充概念-43-梯度下降法
  • Reids之Set类型解读
  • 【网络基础】数据链路层
  • 云计算|OpenStack|使用VMware安装华为云的R006版CNA和VRM---初步使用(二)
  • Python typing函式庫和torch.types
  • UE5 编程规范
  • 交互消息式IMessage扩展开发记录
  • 软件团队降本增效-建立需求评估体系
  • npm yarn pnpm 命令集
  • python 开发环境(PyCharm)搭建指南
  • springboot里 运用 easyexcel 导出
  • 一“码”当先,PR大征集!2023 和RT-Thread一起赋能开源!
  • jmeter模拟多用户并发
  • 澎峰科技|邀您关注2023 RISC-V中国峰会!
  • 【系统架构】系统架构设计之数据同步策略
  • Linux内核学习笔记——ACPI命名空间
  • 使用 OpenCV Python 实现自动图像注释工具的详细步骤--附完整源码
  • RunnerGo中WebSocket、Dubbo、TCP/IP三种协议接口测试详解
  • 【Java 动态数据统计图】动态数据统计思路案例(动态,排序,数组)一(112)
  • kafka踩坑
  • 让你专注于工作的电脑桌面日程提醒软件
  • 62页智慧产业园区数字化综合解决方案PPT
  • 苹果开发者账号注册方法简明指南
  • SQL-每日一题【1321. 餐馆营业额变化增长】
  • PyCharm PyQt5 开发环境搭建
  • 2023-08-17 Untiy进阶 C#知识补充8——C#中的日期与时间
  • SPSS--如何使用分层分析以及分层分析案例分享
  • 时序数据库influxdb笔记