-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetRandomImages.py
69 lines (51 loc) · 2.4 KB
/
getRandomImages.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Program : GetRandomImages.py
Description : This program requests an image from a url and saves it locally.
Additionally, it renames the image name in order to prevent
images being overwritten.
Author : Burak Can Ozter - B00784243 , [email protected]
Date : April 3rd, 2019
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' Libraries '''
import urllib.request
import os
import re
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
*Function Name : save_as()
*Arguments : string file_name
*Return Value : string
*Description : This function checks if the specified file name already exists
and if it does, it increments the file number and saves it in
the current folder.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
def save_as(file_name):
''' File exists '''
if(os.path.exists(file_name)):
print("File already exists...")
print("Renaming file...")
''' Repeat until the file name is unique so it does not overwrite '''
while(os.path.exists(file_name)):
''' File name does not have a number '''
if not (re.search(r'\d+', file_name)):
''' Assign a number '''
new_file_name = file_name.split(".")[0] + "0.png"
file_name = new_file_name
new_file_name = re.search(r'\d+', file_name).group()
else:
''' File name already has a number '''
new_file_name = re.search(r'\d+', file_name).group()
''' Increment file name number.
For instance, xxx9 -> xxx10
'''
file_name = file_name.replace(new_file_name,str(int(new_file_name) + 1))
''' Check if it is a unique name '''
if not(os.path.exists(file_name)):
print("New file has been saved as ", file_name)
return file_name
else:
''' Valid file name'''
print("New image file created as ", file_name)
return file_name
''' Request images from a url '''
urllib.request.urlretrieve("https://picsum.photos/256/256/?random", save_as("pub.png") )
urllib.request.urlretrieve("https://picsum.photos/256/256/?random", save_as("pk.png") )