forked from tim-ojo/python-concurrency-getting-started
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thumbnail_maker.py
118 lines (91 loc) · 4.03 KB
/
thumbnail_maker.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
# thumbnail_maker.py
import time
import os
import logging
from urllib.parse import urlparse
from urllib.request import urlretrieve
from queue import Queue
from threading import Thread
import PIL
from PIL import Image
FORMAT = '[%(threadName)15s%(asctime)25s%(levelname)10s] - %(message)s'
logging.basicConfig(filename='logfile.log', level=logging.DEBUG, format=FORMAT)
class ThumbnailMakerService(object):
def __init__(self, home_dir='.'):
self.home_dir = home_dir
self.input_dir = self.home_dir + os.path.sep + 'incoming'
self.output_dir = self.home_dir + os.path.sep + 'outgoing'
self.img_queue = Queue()
self.dl_queue = Queue()
def download_image(self):
while not self.dl_queue.empty():
try:
url = self.dl_queue.get(block=False)
# download each image and save to the input dir
img_filename = urlparse(url).path.split('/')[-1]
urlretrieve(url, self.input_dir + os.path.sep + img_filename)
self.img_queue.put(img_filename)
self.dl_queue.task_done()
except Queue.empty:
logging.info("Queue empty")
def download_images(self, img_url_list):
# validate inputs
if not img_url_list:
return
os.makedirs(self.input_dir, exist_ok=True)
logging.info("beginning image downloads")
start = time.perf_counter()
for url in img_url_list:
# download each image and save to the input dir
img_filename = urlparse(url).path.split('/')[-1]
urlretrieve(url, self.input_dir + os.path.sep + img_filename)
self.img_queue.put(img_filename)
end = time.perf_counter()
self.img_queue.put(None)
logging.info("downloaded {} images in {} seconds".format(len(img_url_list), end - start))
def perform_resizing(self):
os.makedirs(self.output_dir, exist_ok=True)
logging.info("beginning image resizing")
target_sizes = [32, 64, 200]
num_images = len(os.listdir(self.input_dir))
start = time.perf_counter()
while True:
filename = self.img_queue.get()
if filename:
logging.info(f'resizing image {filename}')
orig_img = Image.open(self.input_dir + os.path.sep + filename)
for basewidth in target_sizes:
img = orig_img
# calculate target height of the resized image to maintain the aspect ratio
wpercent = (basewidth / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
# perform resizing
img = img.resize((basewidth, hsize), PIL.Image.LANCZOS)
# save the resized image to the output dir with a modified file name
new_filename = os.path.splitext(filename)[0] + \
'_' + str(basewidth) + os.path.splitext(filename)[1]
img.save(self.output_dir + os.path.sep + new_filename)
os.remove(self.input_dir + os.path.sep + filename)
logging.info(f'done resizing image: {filename}')
self.img_queue.task_done()
else:
self.img_queue.task_done()
break
end = time.perf_counter()
logging.info("created {} thumbnails in {} seconds".format(num_images, end - start))
def make_thumbnails(self, img_url_list):
logging.info("START make_thumbnails")
start = time.perf_counter()
for img_url in img_url_list:
self.dl_queue.put(img_url)
num_dl_threads = 4
for _ in range(num_dl_threads):
t = Thread(target=self.download_image)
t.start()
t2 = Thread(target=self.perform_resizing)
t2.start()
self.dl_queue.join()
self.img_queue.put(None)
t2.join()
end = time.perf_counter()
logging.info("END make_thumbnails in {} seconds".format(end - start))