-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape_images.py
executable file
·54 lines (44 loc) · 1.22 KB
/
scrape_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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/python3
import urllib.request
import sys
import os
if len(sys.argv) < 2:
print("Arguments missing!")
exit(1)
if sys.argv[1] == "help":
print("""
help: display this page
renumber: renumber the photos in images directory
link [filename]: download images from links in the given filename
""")
if sys.argv[1] == "renumber":
print("Starting image renaming")
files = os.listdir("images")
files.sort()
n = 1
for name in files:
image = name[:-4]
if str(n).zfill(5) != image:
source = "images/" + image + ".jpg"
dest = "images/" + str(n).zfill(5) + ".jpg"
print("Renaming", source, "to", dest)
os.rename(source, dest)
n += 1
exit(0)
if sys.argv[1] == "link":
if len(sys.argv) < 3:
print("No source for image links defined!")
exit(1)
if not os.path.exists("images"):
os.makedirs("images")
image_links = open(sys.argv[1])
n = 0
for image_name in os.listdir("images"):
n = max(n, int(image_name[:-4]))
n += 1
for link in image_links:
link = link.rstrip('\n')
name = str(n).zfill(5)
print("Downloading ", name + ".jpg")
urllib.request.urlretrieve(link, "images/" + name + ".jpg")
n += 1