-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageLoader.py
163 lines (142 loc) · 4.66 KB
/
ImageLoader.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
from __future__ import division
import multiprocessing
from Queue import Empty
from multiprocessing import Queue
from PIL import Image
import time
import os
import sys
import io
# Thanks to @UdacityJeremy for the framework from which this file was made:
# https://github.com/UdacityJeremy/PooledImageloader
def size(image, w, h):
nimage = image
iw, ih = image.size
ratio = min(w / iw, h / ih)
size = int(iw * ratio), int(ih * ratio)
# if ih < h:
# if iw < w:
# nimage = nimage.resize((int(x * 2) for x in size),
# Image.NEAREST)
# nimage.thumbnail(size, Image.ANTIALIAS)
nimage = nimage.resize(size, Image.ANTIALIAS)
return nimage
def worker_main(input_queue, output_queue, testing):
while True:
data = input_queue.get()
iofile, filename, w, h = data
image = Image.open(iofile)
if testing:
with Timer() as g:
image = size(image, w, h)
resizetime = g.msecs
del image
output_queue.put(resizetime)
if not testing:
image = size(image, w, h)
image = {
'pixels': image.tobytes(),
'size': image.size,
'mode': image.mode,
}
imobj = list((filename, image))
print "pushed", filename
output_queue.put(imobj)
class Timer(object):
def __init__(self, verbose=False):
self.verbose = verbose
def __enter__(self):
self.start = time.time()
return self
def __exit__(self, *args):
self.end = time.time()
self.secs = self.end - self.start
self.msecs = self.secs * 1000 # millisecs
if self.verbose:
print 'elapsed time: %f ms' % self.msecs
class ImageLoader:
global testing
OUTPUT_QUEUE_SIZE = 1000
INPUT_QUEUE_SIZE = 100
WORKER_COUNT = 3
def __init__(self):
self.input_queue = Queue(self.INPUT_QUEUE_SIZE)
self.output_queue = Queue(self.OUTPUT_QUEUE_SIZE)
def start(self, folder):
os.chdir(folder)
self.pool = multiprocessing.Pool(self.WORKER_COUNT,
worker_main, (
self.input_queue,
self.output_queue,
testing
))
def stop(self):
self.pool.terminate()
def put(self, filename, w, h):
if testing:
with Timer() as t:
with open(filename, 'rb') as f:
iofile = io.BytesIO(f.read())
opentime = t.msecs
self.input_queue.put((iofile, filename, w, h))
return opentime
if not testing:
with open(filename, 'rb') as f:
iofile = io.BytesIO(f.read())
self.input_queue.put((iofile, filename, w, h))
def get(self, *p, **kw):
try:
imobj = self.output_queue.get_nowait(*p, **kw)
if not testing:
image = imobj[1]
image = Image.frombytes(
image['mode'],
image['size'],
image['pixels'])
imobj[1] = image
# print "readahead", self.output_queue.qsize()
return imobj
except Empty:
e = sys.exc_info()[0]
if e is not Empty:
print e
return "none"
def main():
global testing
print testing
folder = "G:/Pictures/STP/New"
loader = ImageLoader()
pImgList = list()
loader.start(folder)
os.chdir(folder)
uniSource = os.getcwdu()
for fname in os.listdir(uniSource):
path = os.path.join(uniSource, fname)
if os.path.isdir(path):
continue
if fname.endswith(('.jpg', '.png', '.jpeg', '.gif')):
pImgList.append(fname)
print "files: ", len(pImgList)
print "start"
count = 1
opentimer = list()
resizetimer = list()
for each in pImgList:
opener = loader.put(each, 1680, 1050)
opentimer.append(opener)
imgobj = loader.get()
if imgobj is not "none":
resizetimer.append(imgobj)
count += 1
if count % 100 == 0:
print len(resizetimer), "resizes"
resavg = sum(resizetimer) / len(resizetimer)
openavg = sum(opentimer) / len(opentimer)
print "avg resize: ", str(resavg)
print len(opentimer), "opens"
print "avg open: ", str(openavg)
print "done"
testing = False
if __name__ == '__main__':
testing = True
main()