-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_images.py
40 lines (29 loc) · 1.01 KB
/
random_images.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import requests
import json
import os
def create_directory(name):
try:
os.mkdir(name)
except FileExistsError:
print("Error: the folder already exists")
def download_random_images(url, directory_name):
try:
request = requests.get(url)
except requests.exceptions.ConnectionError:
print("There was an error connection")
return None
images = json.loads(request.text)
url_images = []
for i, image in enumerate(images):
url_images.append(image["download_url"])
images_directory = os.path.join(os.getcwd(), directory_name)
for i, image in enumerate(url_images):
url_image = requests.get(image)
print("Downloading...", image)
with open(f"{images_directory}/{i}.jpg", "wb") as image:
image.write(url_image.content)
print("Done")
url = "https://picsum.photos/v2/list?page=3&limit=100"
directory_name = input("Enter a folder name: ")
create_directory(directory_name)
download_random_images(url, directory_name)