URL列表中的大容量图像下载程序
imgdl的Python项目详细描述
IMGDL
python包,用于从 网址。它具有以下功能:
- 下载是使用concurrent.futures多线程的。
- 依赖于持久缓存。已经下载的图像不是 再次下载,除非您强制imgdl这样做。
- 它可以用作命令行实用程序或python库。
- 下载后将图像规格化为jpg格式+rgb模式。
- 自动生成不同大小的缩略图。
- 可以通过从制服中随机抽取超时来分配下载空间 分配。
安装
pip install imgdl
或者,从根项目目录:
pip install .
使用量
下面是一个使用默认配置的简单示例:
fromimgdlimportdownloadurls=['https://upload.wikimedia.org/wikipedia/commons/9/92/Moh_%283%29.jpg','https://upload.wikimedia.org/wikipedia/commons/8/8b/Moh_%284%29.jpg','https://upload.wikimedia.org/wikipedia/commons/c/cd/Rostige_T%C3%BCr_P4RM1492.jpg']paths=download(urls,store_path='~/.datasets/images',n_workers=50)
100%|███████████████████████████████████| 3/3 [00:08<00:00, 2.68s/it]
图像将使用50个线程下载到~/.datasets/images。 函数返回每个图像的路径列表。路径是 构造为{store_data}/{SHA1-hash(url).jpg}。如果出于任何原因 下载失败,imgdl返回一个None作为路径。
注意,如果使用相同的url再次调用download,则 不会再次下载,因为它将首先检查 已经下载。
paths=download(urls,store_path='~/.datasets/images',n_workers=50)
100%|████████████████████████████████| 3/3 [00:00<00:00, 24576.00it/s]
下载是即时的!而且imgdl足够聪明,可以返回 图像路径。
以下是download:
- iterator:唯一的必需参数。通常是URL列表, 但可以是任何类型的迭代器。
- store_path:应存储图像的根路径
- n_workers:要使用的同时线程数
- timeout:url请求应该允许的超时
- thumbs:如果为true,则根据 拇指尺寸
- thumbs_size:这类词典{name:(width,height)} 指示要创建的缩略图大小。
- min_wait:图像下载之间的最短等待时间
- ^ TT19}$:图像下载 之间的最大等待时间
- {TT20}$:代理或用于请求< /LI>的代理列表
- headers:要给requests 的头
- user_agent:用于请求的用户代理
- notebook:如果为真,请使用笔记本版本的tqdm进度条
- debug如果为真,imgdl记录无法下载的URL 如果图像已经存在,请先检查 store_path以避免重复下载。如果你想的话 强制下载,设置为true。
大多数这些参数也可以在找到的config.yaml文件上设置 在启动python进程的目录中。见 config.yaml.example
命令行界面
它还可以用作命令行实用程序:
$ imgdl --help usage: imgdl [-h][-o STORE_PATH][--thumbs THUMBS][--n_workers N_WORKERS][--timeout TIMEOUT][--min_wait MIN_WAIT][--max_wait MAX_WAIT][--proxy PROXY][-u USER_AGENT][-f][--notebook][-d] urls Bulk image downloader from a list of urls positional arguments: urls Text file with the list of urls to be downloaded optional arguments: -h, --help show this help message and exit -o STORE_PATH, --store_path STORE_PATH Root path where images should be stored (default: ~/.datasets/imgdl) --thumbs THUMBS Thumbnail size to be created. Can be specified as many times as thumbs sizes you want (default: None) --n_workers N_WORKERS Number of simultaneous threads to use (default: 50) --timeout TIMEOUT Timeout to be given to the url request (default: 5.0) --min_wait MIN_WAIT Minimum waittime between image downloads (default: 0.0) --max_wait MAX_WAIT Maximum waittime between image downloads (default: 0.0) --proxy PROXY Proxy or list of proxies to use for the requests (default: None) -u USER_AGENT, --user_agent USER_AGENT User agent to be used for the requests (default: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0) -f, --force Force the download even if the files already exists (default: False) --notebook Use the notebook version of tqdm (default: False) -d, --debug Activate debug mode (default: False)
从谷歌下载图像
这是一个如何使用imgdl从google图像搜索下载图像的示例。 我现在用这个来快速建立图像数据集。我的灵感来自于this博客 由pyimagesearch发布。
要求
使用[google]附加要求安装imgdl:
pip install imgdl[google]
下载chrome here的webdriver,并确保它在您的路径中,例如,将它放在/usr/bin或/usr/local/bin中。
sudo cp chromedriver /usr/local/bin/
克隆此存储库,或只是下载google.py脚本。
使用量
您已经准备好从谷歌图像搜索下载图像。下面是用法示例:
$ python google.py "paris by night" -n 600 --interactive Querying google images for'paris by night' Scrolling down five times600 images found. Downloading to /Users/aguirre/Projets/imagedownloader/examples/images 100%|█████████████████████████████|600/600 [01:15<00:00, 7.91it/s]2018-03-04 23:21:52,616 - imgdl.downloader - INFO - 0 images failed to download
第一个参数是要发送到google的查询。使用-n 600,您至少需要600张图像。 默认情况下,google图片查询页面只有100张图片,如果需要更多图片,需要向下滚动。 脚本所做的是使用selenium来模拟一个浏览会话,并在google搜索上向下滚动。 使用--interactive标志,chrome将打开,您将能够看到它是如何向下滚动的为了 获取更多图像。下面是命令行选项的完整列表:
$ python google.py --help usage: google.py [-h][-n N_IMAGES][--interactive][-o STORE_PATH][--thumbs THUMBS][--n_workers N_WORKERS][--timeout TIMEOUT][--min_wait MIN_WAIT][--max_wait MAX_WAIT][--proxy PROXY][-u USER_AGENT][-f][--notebook][-d] query Download images from a google images query positional arguments: query Query string to be executed on google images optional arguments: -h, --help show this help message and exit -n N_IMAGES, --n_images N_IMAGES Number of expected images to download (default: 100) --interactive Open up chrome interactively to see the search results and scrolling action. (default: False) -o STORE_PATH, --store_path STORE_PATH Root path where images should be stored (default: images) --thumbs THUMBS Thumbnail size to be created. Can be specified as many times as thumbs sizes you want (default: None) --n_workers N_WORKERS Number of simultaneous threads to use (default: 40) --timeout TIMEOUT Timeout to be given to the url request (default: 5.0) --min_wait MIN_WAIT Minimum waittime between image downloads (default: 0.0) --max_wait MAX_WAIT Maximum waittime between image downloads (default: 0.0) --proxy PROXY Proxy or list of proxies to use for the requests (default: None) -u USER_AGENT, --user_agent USER_AGENT User agent to be used for the requests (default: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0) -f, --force Force the download even if the files already exists (default: False) --notebook Use the notebook version of tqdm (default: False) -d, --debug Activate debug mode (default: False)
确认
用于测试的图像来自wikimedia commons