-
Notifications
You must be signed in to change notification settings - Fork 35
/
flickr-download.py
195 lines (163 loc) · 6.86 KB
/
flickr-download.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# Flickr Download, by Jeff Heaton (https://www.heatonresearch.com)
# https://github.com/jeffheaton/pyimgdata
# Copyright 2020, MIT License
import flickrapi
import requests
import logging
import logging.config
import os
import configparser
import time
import csv
import sys
from urllib.request import urlretrieve
from PIL import Image
from io import BytesIO
from hashlib import sha256
# https://code.flickr.net/2008/08/19/standard-photos-response-apis-for-civilized-age/
# Nicely formatted time string
def hms_string(sec_elapsed):
h = int(sec_elapsed / (60 * 60))
m = int((sec_elapsed % (60 * 60)) / 60)
s = sec_elapsed % 60
return f"{h}:{m:>02}:{s:>05.2f}"
def is_true(str):
return str.lower()[0] == 't'
class FlickrImageDownload:
def __init__(self):
self.config = configparser.ConfigParser()
self.config.read("config_flickr.ini")
logging.config.fileConfig("logging.properties")
self.config_path = self.config['Download']['path']
self.config_prefix = self.config['Download']['prefix']
self.config_search = self.config['Download']['search']
self.config_update_minutes = int(self.config['Download']['update_minutes'])
self.config_max_download_count = int(self.config['Download']['max_download'])
self.config_license_allowed = [int(e) if e.isdigit() else e
for e in self.config['Download']['license'].split(',')]
self.config_format = self.config['Process']['image_format']
self.config_process = is_true(self.config['Process']['process'])
self.config_crop_square = is_true(self.config['Process']['crop_square'])
self.config_scale_width = int(self.config['Process']['scale_width'])
self.config_scale_height = int(self.config['Process']['scale_height'])
self.config_min_width = int(self.config['Process']['min_width'])
self.config_min_height = int(self.config['Process']['min_height'])
if "sources_file" in self.config['Download']:
self.config_sources_file = self.config['Download']['sources_file']
else:
self.config_sources_file = None
self.flickr=flickrapi.FlickrAPI(
self.config['FLICKR']['id'],
self.config['FLICKR']['secret'],
cache=True)
def reset_counts(self):
self.download_count = 0
self.start_time = time.time()
self.last_update = 0
self.download_count = 0
self.error_count = 0
self.cached = 0
self.sources = []
def load_image(self, url):
try:
response = requests.get(url)
h = sha256(response.content).hexdigest()
img = Image.open(BytesIO(response.content))
img.load()
return img, h
except KeyboardInterrupt:
logging.info("Keyboard interrupt, stopping")
sys.exit(0)
except:
logging.warning(f"Unexpected exception while downloading image: {url}" , exc_info=True)
return None, None
def obtain_photo(self, photo):
url = photo.get('url_c')
license = photo.get('license')
if int(license) in self.config_license_allowed and url:
image, h = self.load_image(url)
if image:
return image
else:
self.error_count += 1
return None
def check_to_keep_photo(self, url, image):
h = sha256(image.tobytes()).hexdigest()
p = os.path.join(self.config_path, f"{self.config_prefix}-{h}.{self.config_format}")
self.sources.append([url,p])
if not os.path.exists(p):
self.download_count += 1
logging.debug(f"Downloaded: {url} to {p}")
return p
else:
self.cached += 1
logging.debug(f"Image already exists: {url}")
return None
def process_image(self, image, path):
width, height = image.size
# Crop the image, centered
if self.config_crop_square and self.config_process:
new_width = min(width,height)
new_height = new_width
left = (width - new_width)/2
top = (height - new_height)/2
right = (width + new_width)/2
bottom = (height + new_height)/2
image = image.crop((left, top, right, bottom))
# Scale the image
if self.config_scale_width>0 and self.config_process:
image = image.resize((
self.config_scale_width,
self.config_scale_height),
Image.ANTIALIAS)
# Convert to full color (no grayscale, no transparent)
if image.mode not in ('RGB'):
logging.debug(f"Grayscale to RGB: {path}")
rgbimg = Image.new("RGB", image.size)
rgbimg.paste(image)
image = rgbimg
return image
def track_progress(self):
elapsed_min = int((time.time() - self.start_time)/60)
self.since_last_update = elapsed_min - self.last_update
if self.since_last_update >= self.config_update_minutes:
logging.info(f"Update for {elapsed_min}: images={self.download_count:,}; errors={self.error_count:,}; cached={self.cached:,}")
self.last_update = elapsed_min
if self.download_count > self.config_max_download_count:
logging.info("Reached max download count")
return True
return False
def write_sources(self):
if self.config_sources_file:
logging.info("Writing sources file.")
filename = os.path.join(self.config_path, self.config_sources_file)
with open(filename, 'w', newline='') as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow(['url', 'file'])
csvwriter.writerows(self.sources)
def run(self):
logging.info("Starting...")
self.reset_counts()
photos = self.flickr.walk(text=self.config_search,
tag_mode='all',
tags=self.config_search,
extras='url_c,license',
per_page=100,
sort='relevance',
#license='0'
)
for photo in photos:
url = photo.get('url_c')
img = self.obtain_photo(photo)
if img:
path = self.check_to_keep_photo(url, img)
if path:
img = self.process_image(img, path)
img.save(path)
if self.track_progress():
break
self.write_sources()
elapsed_time = time.time() - self.start_time
logging.info("Complete, elapsed time: {}".format(hms_string(elapsed_time)))
task = FlickrImageDownload()
task.run()