从网站下载一个图像的多个实例并将其保存在Windows文件夹中

时间:2019-06-25 03:31:54

标签: python python-3.x

我想从thispersondoesnotexist.com下载多个图像并将其保存在Windows文件夹中。该网站使用AI StyleGAN生成了数千张人脸图像,但问题是该网站每个视图仅生成一个图像,该图像被标记为image.jpg,并且每次重置页面时,该单个图像都会更改。

我想编写一个Python脚本,该脚本检索并保存此随机生成的图像的多个实例,最后得到许多不同的图像。

我尝试使用Nandhugp编写的以下脚本:

import urllib.request
import random


n=input("How many images do you need?")
val=int(n)
dir=input("Enter the directory you want to save")
for i in range(val):
   file_name = random.randrange(1,10000)
   full_file_name = dir + str(file_name) + '.jpg' #Insert your
   def downloader(image_url,full_file_name):


     urllib.request.urlretrieve(image_url,full_file_name)

   downloader("https://www.thispersondoesnotexist.com/",full_file_name)

但是,该脚本对我不起作用;抱歉,我是Python 3的新手,并且在理解Windows中的文件路径时遇到一些困难。当脚本询问要保存文件的目录时,我是否输入“ c:\ faces \”或“ c:/ faces /”?

我在较旧的笔记本电脑上使用Win 7 64位操作系统。

1 个答案:

答案 0 :(得分:0)

# python3
import requests
def download(fileName):
    f = open(fileName,'wb')
    f.write(requests.get('https://thispersondoesnotexist.com/image', headers={'User-Agent': 'My User Agent 1.0'}).content)
    f.close()

for i in range(2000):
    download(str(i)+'.jpg')
//node js
var request = require('request');

var fs = require('fs');
var sleep = function (duration) {
    return new Promise(resolve => {
        setTimeout(() => resolve(), duration)
    });
}
var downloadFile = function (url, fileName) {
    return new Promise(resolve => {
        var req = request(url);
        var file = fs.createWriteStream(fileName);
        req.pipe(file);
        req.on('end', () => {
            resolve();
        })
    })
}
var main = async function () {
    for (var i = 0; i < 100; i++) {
        console.log('Downloading ' + i)
        await downloadFile('https://thispersondoesnotexist.com/image', `${i}.jpg`)
        await sleep(1000);
    }
}

main();